'compare dates using momentjs
I have a date timestamp stored in mysql which is like 2017-06-29 08:37:31
I am getting it through AJAX and I want to compare it with current date, whether it is same or not.
My code is
let dateStamp = moment(this.notificationData[0].notification_timestamp).format('LL');//date/timestamp from mysql received in json
let dateToday = moment().format('LL');//current date
console.log(dateStamp,dateToday)
console.log(moment(dateToday).diff(dateStamp,'days'))//comparison
Though I am getting the result in .diff(). I want to know if there is any other correct way of doing this.
Timezone is not an issue I think for me. Just date comparison.
Thanks
Solution 1:[1]
You can compare them directly.
var date = moment("2017-07-11")
var now = moment();
if (now > date) {
// date is past
} else {
// date is future
}
You can also use isBefore, isSame, and isAfter.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rachit |