javascript 在节点中获取文件创建日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17552017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:44:58  来源:igfitidea点击:

Get file created date in node

javascriptnode.jsdatefilesystems

提问by wheresrhys

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_statsdocumentation at all.

在节点中,我可以使用标准文件统计对象获取文件的修改日期。有没有办法我也可以找到它的创建日期?我在http://nodejs.org/api/fs.html#fs_class_fs_stats文档中根本找不到任何内容。

采纳答案by aleatha

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.

能否获得文件创建时间取决于操作系统和文件系统。传统的 POSIX 只定义了 ctime,它是(相当混乱),inode 修改日期,而不是其他人提到的创建日期。但是,在某些操作系统上,您可以获得 st_birthtimespec 或 st_birthtime,这是真正的“创建”时间。您需要检查主机操作系统上的 sys/stat.h 以查看可用的内容(如果有)。

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.

不幸的是,您是否可以从 node.js 访问整个 stat 结构是另一回事。但至少你可以弄清楚你的操作系统是否支持它并从那里开始。

2019 Update:

2019 更新:

You can always access this property, but on many file systems it will be wrong. According to the Node.js docs:

您始终可以访问此属性,但在许多文件系统上它会出错。根据 Node.js 文档:

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

在出生时间不可用的文件系统上,该字段可能改为保存 ctime 或 1970-01-01T00:00Z(即 Unix 纪元时间戳 0)。在这种情况下,该值可能大于 atime 或 mtime。在 Darwin 和其他 FreeBSD 变体上,如果使用 utimes(2) 系统调用将 atime 显式设置为比当前出生时间更早的值,也要设置。 https://nodejs.org/api/fs.html#fs_stats_birthtimems

回答by flaky

If someone stumbles over this after all this time, as of Node v0.12.0 use this:

如果有人在这么长时间后偶然发现了这一点,从 Node v0.12.0 开始,请使用以下命令:

fs.stat(path, callback)

Where callback has two args err & stats. Stats object has property

回调有两个 args err & stats。统计对象具有属性

birthtime

which is creation date.

这是创建日期。

Link to node api documentation link

链接到节点 api 文档链接

回答by Zbigniew

If you are using Linux then this information is not accessible (though it is stored in Ext4 file system). Thus fs.statonly returns atime, ctime, mtimevalues.

如果您使用的是 Linux,则无法访问此信息(尽管它存储在 Ext4 文件系统中)。因此fs.stat只返回atime, ctime,mtime值。

回答by Patrick

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.

我正在使用 Fedora,但 fs.stat 结果中没有“出生时间”字段。但是也许您可以尝试使用节点的 child_process 来调用 ls --full-time。至少在我的机器上它给了我正确的结果。

fs.stat result:

fs.stat 结果:

{ 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

ls --全职

  -rwxrwxrwx. 1 pahuang pahuang 22M 2013-06-23 17:51:08.000000000 +1000 test_media/processed/20130621.mkv