📜  Node.js stats.ctime 属性

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

Node.js stats.ctime 属性

stats.ctime属性是 fs.Stats 类的内置应用程序编程接口,用于获取上次更改文件状态时的时间戳。

句法:

stats.ctime

返回值:它返回一个日期值,表示上次更改文件状态时的时间戳。

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

示例 1:

// Node.js program to demonstrate the   
// stats.ctime property
  
// Accessing fs module
const fs = require('fs');
  
// Calling stats.ctime property from
// fs.Stats class using stat
fs.stat('./', (err, stats) => {
    if (err) throw err;
  
    // The timestamp when the file status
    // has been changed last time
    console.log("Using stat: " + stats.ctime);
});
  
// Using lstat
fs.lstat('./filename.txt', (err, stats) => {
    if (err) throw err;
  
    // The timestamp when the file status
    // has been changed last time 
    console.log("Using lstat: " + stats.ctime);
});

输出:

Using stat: Sun Jun 21 2020 01:17:13 GMT+0530 (India Standard Time)
Using lstat: Sun Jun 21 2020 01:19:02 GMT+0530 (India Standard Time)

示例 2:

// Node.js program to demonstrate the   
// stats.ctime property
  
// Accessing fs module
const fs = require('fs').promises;
   
// Calling fs.Stats stats.ctime
(async() => {
  const stats = await fs.stat('./filename.txt');
   
  // The timestamp when the file status
  // has been changed last time 
  console.log("Using stat synchronous: "
                + stats.ctime);
})().catch(console.error)

输出:

(node:2748) ExperimentalWarning: The fs.promises API is experimental
Using stat synchronous: Sun Jun 21 2020 01:19:02 GMT+0530 
                                     (India Standard Time)

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

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