Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

javascript - Moment.js Check a date is today

How do I check a date is actually today (the same date) rather than the difference between hours in a day?

I have three timestamps as examples, one is today (22/07/14) and the other two are yesterday (21/07/14)

1406019110000 // today
1405951867000 // yesterday
1405951851000 // yesterday

I have tried this but all return false:

timestamp = moment.tz(timestamp, tz.name());    
var today = moment('DD/MM/YYYY').isAfter(timestamp, 'DD/MM/YYYY');
console.log(today);
question from:https://stackoverflow.com/questions/24883760/moment-js-check-a-date-is-today

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use isSame(), limiting the granularity to a day:

var today = moment(1406019110000);
var yesterday = moment(1405951867000);

if (today.isSame(yesterday, 'd')) {
    // They are on the same day
} else {
    // They are not on the same day
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...