EN
Node.js - get last modified date of file
0
points
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