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


node.js stats.mtimeNs用法及代碼示例

stats.mtimeNs屬性是fs.Stats類的內置應用程序編程接口,用於獲取自POSIX紀元以來的最後一次修改文件以來的時間戳(以納秒表示)。

用法:

stats.mtimeNs;

參數:屬性沒有任何參數,但是在創建統計信息時,對象{bigint:true}必須作為選項傳遞。

返回值:它返回一個數字或BigInt值,該值表示自POSIX曆元以來的最後一次修改文件以來的時間戳(以納秒為單位)。

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



範例1:

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

輸出:

Using stat:1592665056784809
Using lstat:15926651836820176

範例2:

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

輸出:

Using stat synchronous:15926651836820176

注意:上麵的程序將通過使用以下命令進行編譯和運行node filename.js命令並正確使用file_path。

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




相關用法


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