1

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?

Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
kodaek99
  • 135
  • 2
  • 13

1 Answers1

1

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.

rsp
  • 107,747
  • 29
  • 201
  • 177