last modified file date in node.js

后端 未结 4 782
悲&欢浪女
悲&欢浪女 2020-12-24 10:35

I\'m trying to retrieve the last modified date of a file on the server using node.js.

I\'ve tried

file.lastModified;

and

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 10:58

    Here you can get the file's last modified time in seconds.

    fs.stat("filename.json", function(err, stats){
        let seconds = (new Date().getTime() - stats.mtime) / 1000;
        console.log(`File modified ${seconds} ago`);
    });
    

    Outputs something like "File modified 300.9 seconds ago"

提交回复
热议问题