简体   繁体   English

如何在Node.js中使用文件系统获取文件创建日期?

[英]How to get file created date using file System in nodejs?

I want to send to client filename and file created date, I tried to use fs.stat that provides birthtime but i dont see filename in there, SO my question, is birthtime is file created date ? 我想发送给客户端文件名和文件创建日期,我尝试使用提供生日的fs.stat,但是我没有在其中看到文件名,所以我的问题是文件创建日期是生日吗?

How can i send filename and date created as json ? 如何发送创建为json的文件名和日期?

app.js app.js

var readDirectory = require('./readDirectory');
app.get('/logs',function(req,res){
    readDirectory.readDirectory(function(logFiles){
        res.json(logFiles);
    });
});

readDirectory.js readDirectory.js

var fs = require('fs');
var path = './logs/ditLogs'
function readDirectory(callback){
    fs.stat(path, function (err,stats) {
        console.log('STATS',stats);
        callback(stats);
    });

}
exports.readDirectory = readDirectory;

In Node v0.12.0 , yes birthtime is the file created time Node v0.12.0 ,yes birthtime是文件创建时间

Check Node.js API details here 在此处查看Node.js API详细信息

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. 传统的POSIX仅定义ctime,这(相当令人困惑)是inode修改日期,而不是其他人提到的创建日期。 However, on some operating systems, you can get st_birthtimespec, or st_birthtime which is a true "create" time. 但是,在某些操作系统上,您可以获得st_birthtimespec或st_birthtime,这是一个真正的“创建”时间。 You'll need to check sys/stat.h on your host operating system to see what, if anything, is available. 您需要在主机操作系统上检查sys / stat.h,以查看可用的内容。

Unfortunately, whether or not you can access the entire stat structure from node.js is a different kettle of fish. 不幸的是,是否可以从node.js中访问整个stat结构是另一回事。 But at least you can figure out if your OS even supports it and go from there. 但是至少您可以弄清楚您的操作系统是否支持它并从那里开始。

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. 其中callback具有两个args err和stats。 Stats object has property 统计对象具有属性

birthtime

which is creation date. 这是创建日期。

Link to node api documentation https://nodejs.org/api/fs.html#fs_class_fs_stats 链接到节点api文档https://nodejs.org/api/fs.html#fs_class_fs_stats

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM