當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js stats.birthtimeMs屬性用法及代碼示例

stats.birthtimeMs屬性是fs.Stats類的內置應用程序編程接口,用於獲取自POSIX紀元以來創建文件的時間戳(以毫秒為單位)。

用法:

stats.birthtimeMs;

返回值:它返回一個數字或BigInt值,該值表示自POSIX時期以來創建文件時的時間戳(以毫秒為單位)。

以下示例說明了Node.js中stats.birthtimeMs屬性的用法:

範例1:



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

輸出:

using stat:1589375311991.9453
using lstat:1592667100334.387

範例2:

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

輸出:

using stat synchronous:1592667100334.387

注意:上麵的程序將通過使用node filename.js命令並正確使用file_path。

參考: https://nodejs.org/api/fs.html#fs_stats_birthtimems




相關用法


注:本文由純淨天空篩選整理自anwesha0107大神的英文原創作品 Node.js stats.birthtimeMs Property from fs.Stats Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。