Languages
[Edit]
EN

Node.js - get last modified date of file

0 points
Created by:
Maison-Humphries
791

In this article, we would like to show you how to get last modified date of file in Node.js.

Quick solution:

const fs = require('fs');

// path to file
const filePath = './file.txt';

// get last modification date
const stats = fs.statSync(filePath);
const result = stats.mtime;

console.log(result); // 2021-09-11T09:12:05.051Z

 

Practical example

In this example, we create a reusable function that returns last file modification date.

const fs = require('fs');

const filePath = './file.txt';

const getLastModificationDate = (path) => {
  const stats = fs.statSync(path);
  return stats.mtime;
};

console.log(getLastModificationDate(filePath));

Example output:

2021-09-11T09:12:05.051Z

References

Alternative titles

  1. Node.js - get file last updated date
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join