How to compare two fileStat.mtime files in nodejs?

See code:

var fs = require('fs'); var file = "e:/myfile.txt"; fs.stat(file, function(err, stat1) { console.log(stat1.mtime); fs.stat(file, function(err, stat2) { console.log(stat2.mtime); console.log(stat1.mtime == stat2.mtime); console.log(stat1.mtime === stat2.mtime); }); }); 

And the result:

 Sun, 20 May 2012 15:47:15 GMT Sun, 20 May 2012 15:47:15 GMT false false 

I did not modify the file at runtime. But you cannot see == or === , they are not equal.

How to compare two mtime in nodejs?

+6
source share
2 answers

== when testing objects, if the objects are equal. However, < and > do the work for Date objects, so you can simply use this function to compare two objects:

 function datesEqual(a, b) { return !(a > b || b > a); } 
+2
source

Use date.getTime() to compare:

 function datesEqual(a, b) { return a.getTime() === b.getTime(); } 
+10
source

Source: https://habr.com/ru/post/916203/


All Articles