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


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




相關用法


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