📜  Node.js stats.birthtimeNs 属性(1)

📅  最后修改于: 2023-12-03 15:17:55.664000             🧑  作者: Mango

Node.js stats.birthtimeNs 属性

在 Node.js 中,stats.birthtimeNs 属性用于获取一个文件的创建时间(以纳秒为单位)。它是 fs.Stats 对象的一个属性,该对象通过 fs.stat()fs.fstat() 方法返回。

语法
stats.birthtimeNs
返回值

stats.birthtimeNs 属性返回一个表示文件创建时间的纳秒级时间戳。

示例
const fs = require('fs');

fs.stat('file.txt', (err, stats) => {
  if (err) {
    console.error(err);
    return;
  }
  
  console.log('Birthtime (nanoseconds):', stats.birthtimeNs);
});

上述代码会打印出 file.txt 文件的创建时间的纳秒级时间戳。

备注
  • stats.birthtimeNs 属性返回的时间戳是一个 bigint 类型,可以通过其他方法转换为更常见的时间格式。
  • 在一些操作系统中,文件创建时间(birthtime)可能无法获取,此时 stats.birthtimeNs 属性将返回 undefined

参考文档:Node.js fs.Stats