How to check if today and specific date is same using moment - javascript

I want to check if today and a specific date is same using moment.
const today = moment(new Date()).format('DD/MM/YYYY')
console.log(today) // "28/09/2021"
const expiry = moment(new Date('2021/09/28')).format('DD/MM/YYYY')
console.log(expiry) // "28/09/2021"
Now When I compare , I am getting false
console.log(moment(today).isSame(expiry)); // false
It also showing me this in fiddle,
"Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments:

You can compare it directly by operator ==
const today = moment(new Date()).format('DD/MM/YYYY')
console.log(today) // "28/09/2021"
const expiry = moment(new Date('2021/09/28')).format('DD/MM/YYYY')
console.log(expiry) // "28/09/2021"
console.log(today == expiry);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

const today = moment(new Date()).format('DD/MM/YYYY')
console.log(today) // "28/09/2021"
const expiry = moment(new Date('2021/09/28')).format('DD/MM/YYYY')
console.log(expiry) // "28/09/2021"
It's because here, you're setting today and expiry to strings, eg "21/12/2021"
and then asking moment to come strings when it's expecting dates.
Also need to add the flag 'day' to isSame
Try this instead
const today = moment(new Date());
console.log(today.format('DD/MM/YYYY')) // "28/09/2021"
const expiry = moment(new Date('2021/09/28'));
console.log(expiry.format('DD/MM/YYYY'));
console.log(moment(today).isSame(expiry, 'day'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Related

31st date showing invalid date when using moment

I am finding the difference between 2 dates in years, but when I select the 31st date it shows an invalid date so the difference is NaN.
When I use other dates it shows the correct result.
const selectedValue = moment('31-8-2022');
const today = moment();
const yearDiff = today.diff(selectedValue, "year");
console.log(yearDiff);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
When running sample code you provided, a deprecation warning is raised:
Deprecation warning: value provided is not in a recognized RFC2822 or
ISO format. moment construction falls back to js Date(), which is not
reliable across all browsers and versions. Non RFC2822/ISO date
formats are discouraged and will be removed in an upcoming major
release. Please refer to
http://momentjs.com/guides/#/warnings/js-date/ for more info.
By inputing the date in ISO format, the code works:
const selectValue = moment('2022-08-31'); // 2022-08-31 instead of 31-8-2022
const today = moment();
const yearDiff = today.diff(selectValue, "year");
console.log(yearDiff);
when you use 'DD-MM-YYYY' in momentjs you get invalid date format.
const selectedValue = moment('30-08-2022').format('DD-MM-YYYY');
console.log(selectedValue)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
In order to make it valid date, you'll add 'DD-MM-YYYY' in the moment second paramater after the date and format it.
const selectedValue = moment('30-08-2022','DD-MM-YYYY').format('DD-MM-YYYY');
console.log(selectedValue)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
In instance you really have to use 'DD-MM-YYYY' in your code without the worry of depreciation warning. You could do this
const selectedValue = moment('30-08-2022', 'DD-MM-YYYY').format('DD-MM-YYYY');
console.log(selectedValue)
const today = moment().format('DD-MM-YYYY');
console.log(today)
const yearDiff = moment(today, 'DD-MM-YYYY').diff(moment(selectedValue, 'DD-MM-YYYY'), 'years');
console.log(yearDiff);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>

Comparing date and time with moment js

In my react app I am receiving an endDate, and also separately receiving an endTime. I have to check if that endDate and endTime are before the current date and time.
let endDate = "04/13/2022";
let endTime = "22:00"
console.log(moment(endDate, "MM/DD/YYYY", endTime).isBefore(moment());
//true
Today's date is the same as the endDate but the time is earlier than the endTime so I should see False instead of true. The times are not being compared. Does anyone know how to resolve this?
Combine the date and time strings, and parse as one full date-time string.
const endDate = '04/13/2022';
const endTime = '22:00';
const date = moment(`${endDate} ${endTime}`, 'MM/DD/YYYY HH:mm');
console.log(date.isBefore(moment())); // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js"></script>

date comparison moment js

I want to compare two dates and my date format is DD-MM-YYYY but I don't know why my output returns false when I compare my dates.
example 1
const date1 = '30-06-2021';
const date2 = '10-01-2022';
const result = moment(date1) < moment(date2); // return false, should return true
example 2
const date1 = '30-06-2021';
const date2 = '10-01-2022';
const result = moment(date1).isBefore(date2); // return false, should return true
There are two issues there:
You're expecting moment to guess the format of your dates, but it can't do that reliably. Always provide a format string if your string isn't in a RFC2822 or ISO-8601 format. moment itself warns you about doing that in the dev version of the library:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Please refer to http://momentjs.com/guides/#/warnings/js-date/
< cannot be used to meaningfully compare objects. If you want to know if a date is before another date, use the isBefore method.
For example:
const date1 = "30-06-2021";
const date2 = "10-01-2022";
const format = "DD-MM-YYYY";
const result = moment(date1, format).isBefore(moment(date2, format));
const date1 = "30-06-2021";
const date2 = "10-01-2022";
const format = "DD-MM-YYYY";
const result = moment(date1, format).isBefore(moment(date2, format));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

How to check if date is a current date with using dayjs?

I have a problem with checking if my date in unix is a current date with using dayjs library, I try like below:
const date = 1631978008; //today: 2021-09-18
const isToday = dayjs().isSame(date, 'day'); //return false
but always return me false when my date is a today date, can someone tell me why? it should return true :/
thanks for any help!
You can use the unix function to parse the unix timestamp in seconds before comparing it with the current time.
const date = 1631978008; //today: 2021-09-18
const isToday = dayjs().isSame(dayjs.unix(date), 'day');
console.log(isToday);
<script src="https://unpkg.com/dayjs#1.8.21/dayjs.min.js"></script>

Moment JS from yyyy-mm-ddthh:mm:ss to MM-DD-YY mm:ss [duplicate]

I want to parse the following string with moment.js 2014-02-27T10:00:00 and output
day month year (14 march 2014)
I have been reading the docs but without success
http://momentjs.com/docs/#/parsing/now/
I always seem to find myself landing here only to realize that the title and question are not quite aligned.
If you want a moment date from a string:
const myMomentObject = moment(str, 'YYYY-MM-DD')
From moment documentation:
Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object.
If you instead want a javascript Date object from a string:
const myDate = moment(str, 'YYYY-MM-DD').toDate();
You need to use the .format() function.
MM - Month number
MMM - Month word
var date = moment("2014-02-27T10:00:00").format('DD-MM-YYYY');
var dateMonthAsWord = moment("2014-02-27T10:00:00").format('DD-MMM-YYYY');
FIDDLE
No need for moment.js to parse the input since its format is the standard one :
var date = new Date('2014-02-27T10:00:00');
var formatted = moment(date).format('D MMMM YYYY');
http://es5.github.io/#x15.9.1.15
moment was perfect for what I needed. NOTE it ignores the hours and minutes and just does it's thing if you let it. This was perfect for me as my API call brings back the date and time but I only care about the date.
function momentTest() {
var varDate = "2018-01-19 18:05:01.423";
var myDate = moment(varDate,"YYYY-MM-DD").format("DD-MM-YYYY");
var todayDate = moment().format("DD-MM-YYYY");
var yesterdayDate = moment().subtract(1, 'days').format("DD-MM-YYYY");
var tomorrowDate = moment().add(1, 'days').format("DD-MM-YYYY");
alert(todayDate);
if (myDate == todayDate) {
alert("date is today");
} else if (myDate == yesterdayDate) {
alert("date is yesterday");
} else if (myDate == tomorrowDate) {
alert("date is tomorrow");
} else {
alert("It's not today, tomorrow or yesterday!");
}
}
How to change any string date to object date (also with moment.js):
let startDate = "2019-01-16T20:00:00.000";
let endDate = "2019-02-11T20:00:00.000";
let sDate = new Date(startDate);
let eDate = new Date(endDate);
with moment.js:
startDate = moment(sDate);
endDate = moment(eDate);
Maybe try the Intl polyfill for IE8 or the olyfill service ?
or
https://github.com/andyearnshaw/Intl.js/

Categories

Resources