Home > Mobile >  Why is Node.js fs.statSync birthtime in Node.js off?
Why is Node.js fs.statSync birthtime in Node.js off?

Time:10-31

I have two questions that I haven't seen answers for on this matter, nor do I recall a heads up in the docs.

  1. Why are fs.statSync datetime stamps such as birthtime all off and not matching what I see when I view the files manually in my OS (which shows the correct datetimes)?

  2. How do I get the EXACT correct times, exactly as they are shown on the file itself? I know the dates and times are correct when I view it in the OS because I was there for the photo. Just don't know what to do to make Node.js give me the times as they really were/are.

Here's my correct output in the OS (Windows in this case, but it needs to work the same in all OSs):

enter image description here

enter image description here

Here's the call I'm making within seconds of the other:

const stats = fs.statSync(absPathOfFile);
const dates = [
    stats.birthtime,
    stats.ctime,
    stats.mtime,
    stats.atime,
];

And that Node.js output:

[
  2022-10-31T08:47:00.900Z,
  2022-06-13T05:37:42.128Z,
  2022-04-12T04:55:49.070Z,
  2022-10-31T08:47:02.027Z
]

So all of those dates are off. I was expecting Node.js to return the actual dates/times as listed in the file's meta data.

CodePudding user response:

To make the comment discussion an answer:

  • birthtime is not necessarily available on all file systems, and how the atime/mtime/ctime/birthtime fields map to file system properties depends. Node.js docs here.
  • The "Date Taken" that Windows (and maybe other OSes) shows is Exif metadata pulled from the file's internal data itself, and is not related to a filesystem date. (IOW, moving the file around or resetting its times with e.g. touch or other calls doesn't affect it.)
  • The dates returned by statSync are in UTC time (which is evident from the Z time zone specifier in the ISO8601 formatted output). Windows shows the dates in the user's local time zone.
  • Related