kodaek99
kodaek99

Reputation: 135

How to convert date to the specified string format in nodejs?

I'm reading a file's modification date with fs.statSync() and I'd like to compare it with a String that comes from my database:

This is my file's last modification date:

Fri Mar 24 2017 13:22:01 GMT+0100 (Central Europe Standard Time)

And I'd like to compare with this string:

2016-07-18 12:28:12

How can I do this with Node.JS? Can I somehow create a new date from the String?

Upvotes: 1

Views: 1460

Answers (1)

rsp
rsp

Reputation: 111316

You can use Moment parse both of those formats and compare the results.

var date1 = moment(string1, format1, true);
var date2 = moment(string2, format2, true);
if (date1.diff(date2) === 0) {
    // equal
} else {
    // not equal
}

See this answer for parsing dates in different formats:

Make sure that you parse it with explicit format and you don't let it guess the format, or otherwise you will never be sure if it guessed correctly.

Upvotes: 1

Related Questions