Skip to content
Related Articles

Related Articles

Improve Article
Node.js | stats.ctime Property
  • Last Updated : 23 Jun, 2020

The stats.ctime property is an inbuilt application programming interface of the fs.Stats class that is used to get the timestamp when the file status has been changed last time.

Syntax:

stats.ctime

Return Value: It returns a date value that represents the timestamp when the file status has been changed last time.

Below examples illustrate the use of stats.ctime property in Node.js:

Example 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);
});

Output:

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)

Example 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)

Output:

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

Note: The above program will compile and run by using the node filename.js command and use the file_path correctly.

Reference: https://nodejs.org/api/fs.html#fs_stats_ctime




My Personal Notes arrow_drop_up
Recommended Articles
Page :