Reputation: 149
I have the following dates
var date1 = moment(new Date(2019, 4, 17).valueOf());
var date2 = moment(new Date(2019, 4, 17).valueOf());
if i use
if (date1.isSame(date2)) {
c.log('same')
} else {
c.log('not same')
// They are not on the same day
}
then they will be same.
But how can i match - true when the year is different
var date1 = moment(new Date(2029, 4, 17).valueOf());
var date2 = moment(new Date(2019, 4, 17).valueOf());
this should give true because they are both on 17 day.Even the year is different. This should also remain true
var date1 = moment(new Date(2019, 4, 17).valueOf());
var date2 = moment(new Date(2019, 4, 17).valueOf());
Upvotes: 0
Views: 138
Reputation: 7115
you could compare month and day to check if they're same, no matter the year:
if (date1.format('M') === date2.format('M') && date1.format('D') === date2.format('D'))
console.log('day and month are the same')
EDIT: changed bug in condition
Upvotes: 0
Reputation: 5623
You should check if month
which is represented by the format token M and day of month
represented by the format token D are the same like this
if(date1.format('M') === date2.format('M') && date1.format('D') === date2.format('D')){
// Your code goes here after
}
If they are the same that means It's the same day of the same month whatever the year
Upvotes: 0
Reputation: 41
You can try this
var date1 = moment(new Date(2019, 4, 17).valueOf()).format('MM/DD');
var date2 = moment(new Date(2019, 4, 17).valueOf()).format('MM/DD');
if (date1 === date2)
console.log('true');
else
console.log('false');
Upvotes: 1