In node I can get a file's modified date using the standard file stat object. Is there a way I can also find its created date? I can't find anything in the http://nodejs.org/api/fs.html#fs_class_fs_stats documentation at all.
问题:
回答1:
Whether or not you can get the file creation time depends on the OS and file system. Traditional POSIX only defines ctime, which is (rather confusingly), the inode modification date, not the creation date, as other people have mentioned. However, on some operating systems, you can get st_birthtimespec, or st_birthtime which is a true "create" time. You'll need to check sys/stat.h on your host operating system to see what, if anything, is available.
Unfortunately, whether or not you can access the entire stat structure from node.js is a different kettle of fish. But at least you can figure out if your OS even supports it and go from there.
2019 Update:
You can always access this property, but on many file systems it will be wrong. According to the Node.js docs:
On filesystems where birthtime is not available, this field may instead hold either the ctime or 1970-01-01T00:00Z (ie, Unix epoch timestamp 0). This value may be greater than atime or mtime in this case. On Darwin and other FreeBSD variants, also set if the atime is explicitly set to an earlier value than the current birthtime using the utimes(2) system call. https://nodejs.org/api/fs.html#fs_stats_birthtimems
回答2:
If someone stumbles over this after all this time, as of Node v0.12.0 use this:
fs.stat(path, callback)
Where callback has two args err & stats. Stats object has property
birthtime
which is creation date.
Link to node api documentation link
回答3:
If you are using Linux then this information is not accessible (though it is stored in Ext4 file system). Thus fs.stat
only returns atime
, ctime
, mtime
values.
回答4:
I am using Fedora and there is no 'birthtime' field in the fs.stat result. But maybe you can try using node's child_process to invoke ls --full-time. At least on my machine it's giving me correct result.
fs.stat result:
{ dev: 64771,
mode: 33279,
nlink: 1,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: 2098445,
size: 48523700,
blocks: 94776,
atime: Sat Jul 04 2015 19:01:29 GMT+1000 (AEST),
mtime: Thu Aug 22 2013 16:45:10 GMT+1000 (AEST),
ctime: Sat Jul 04 2015 19:01:29 GMT+1000 (AEST) }
ls --full-time
-rwxrwxrwx. 1 pahuang pahuang 22M 2013-06-23 17:51:08.000000000 +1000 test_media/processed/20130621.mkv