HomeTutorsContact

How to get last modified date of a file using Node.js?

By Gulshan Saini
Published in NodeJS
May 26, 2020
1 min read

Node.js fs i.e. file system module allows interacting with the file system on your computer or server. All the file system operations support synchronous and asynchronous methods.

To get the last modified date of a file or directory, Node.js provides the following operations

  • fs.stat - asynchronous operation
  • fs.statSync - synchronous operation

Using fs.stat

fs.stat is an asynchronous operation and requires (path, callback) arguments

  • path is valid file path
  • callback takes two arguments i.e. (err, stats) where stats is fs.Stats object

Let’s see it in action

// require file system module
const fs = require('fs');

fs.stat('/Users/gulshansaini', function(err, stats) {
    console.log(stats);
});

Save the above code in a file and name it index.js - make sure you change the path of the file. Open the terminal and go to the directory where index.js was saved and run

    node index.js

After, execution it should return following output

Stats {
  dev: 16777223,
  mode: 16877,
  nlink: 44,
  uid: 501,
  gid: 20,
  rdev: 0,
  blksize: 4194304,
  ino: 686089,
  size: 1408,
  blocks: 0,
  atimeMs: 1590507277048.7937,
  mtimeMs: 1590507277015.7002,
  ctimeMs: 1590507277015.7002,
  birthtimeMs: 1589824465325.0923,
  atime: 2020-05-26T15:34:37.049Z,
  mtime: 2020-05-26T15:34:37.016Z,
  ctime: 2020-05-26T15:34:37.016Z,
  birthtime: 2020-05-18T17:54:25.325Z }

Out of the values returned in the previous output, mtime and mtimeMs are the ones which we are more interested in.

mtime and mtimeMs provide timestamp indicating the last time this file was modified and their value is updated when contents of file or directory are changed.

mtime returns updated date-time of a file in human-readable format whereas, mtimeMs timestamp indicates the last time this file was modified expressed in milliseconds since the POSIX Epoch.

Tip: You can even destructure the object and filter out only the properties you are interested in as follows

// require file system module
const fs = require('fs');

fs.stat('/Users/gulshansaini', function(err, {mtime}) {
    console.log(`Last modified time ${mtime}`);
});

This would return the following output

Last modified time Tue May 26 2020 21:17:51 GMT+0530 (India Standard Time)

Using fs.statSync

fs.statSync is synchronous operation that takes a file path as input and it returns fs.Stats object similar to its asynchronous counterpart as follows

Let’s see it in action

const fs = require('fs');

// destructure the fs.Stats object
const { mtime } = fs.statSync('/Users/gulshansaini');
console.log(`Last modified time ${mtime}`);

Save the above code in a file and name it index.js - make sure you change the path of the file. Open a terminal and go to the directory where index.jswas saved and run

    node index.js

This would return following output

Last modified time Tue May 26 2020 21:28:51 GMT+0530 (India Standard Time)

Tags

#nodejs
Previous Article
How do you capitalize letters in CSS?

Related Posts

NodeJS
What is the max HTTP header size in NodeJS server?
August 17, 2020
1 min
Gulshan Saini

Gulshan Saini

Fullstack Developer

Topics

Angular
JavaScript
ReactJS
Typescript

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.
© 2021, All Rights Reserved.

Quick Links

Contact UsBrowserCSSPythonPuppeteer

Social Media