📌  相关文章
📜  fs.Stats 类的 Node.js stats.mtimeMs 属性

📅  最后修改于: 2022-05-13 01:56:40.628000             🧑  作者: Mango

fs.Stats 类的 Node.js stats.mtimeMs 属性

stats.mtimeMs 属性是 fs.Stats 类的内置应用程序编程接口,用于获取自 POSIX 纪元以来文件最后一次修改时的时间戳,以毫秒表示。

句法:

stats.mtimeMs;

参数:此属性不接受任何参数。

返回值:它返回一个数字或 BigInt 值,表示自 POSIX 纪元以来上次修改文件时的时间戳,以毫秒表示。

以下示例说明了 Node.js 中 stats.mtimeMs 属性的使用:
示例 1:

// Node.js program to demonstrate the   
// stats.mtimeMs Property
  
// Accessing fs module
const fs = require('fs');
  
// Calling fs.Stats stats.mtimeMs
// property using stat
fs.stat('./', (err, stats) => {
    if (err) throw err;
  
    // The timestamp when the file
    // is last modified (in MS) 
    console.log("Using stat: " + stats.mtimeMs);
});
  
// Using lstat
fs.lstat('./filename.txt', (err, stats) => {
    if (err) throw err;
  
    // The timestamp when the file 
    // is last modified (in MS)
    console.log("Using lstat: " 
            + stats.mtimeMs);
});

输出:

Using stat: 1592665056784.809
Using lstat: 1592665183682.0176

示例 2:

// Node.js program to demonstrate the   
// stats.mtimeMs Property
  
// Accessing fs module
const fs = require('fs').promises;
  
// Calling fs.Stats stats.mtimeMs
(async () => {
    const stats = await fs.stat('./filename.txt');
  
    // The timestamp when the file
    // is last modified (in MS) 
    console.log("using stat synchronous: "
                + stats.mtimeMs);
})().catch(console.error)

输出:

Using stat synchronous: 1592665183682.0176

注意:以上程序将通过使用node filename.js命令编译运行,并正确使用 file_path。

参考: https://nodejs.org/api/fs.html#fs_stats_mtimems