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:
xxxxxxxxxx
1
const fs = require('fs');
2
3
// path to file
4
const filePath = './file.txt';
5
6
// get last modification date
7
const stats = fs.statSync(filePath);
8
const result = stats.mtime;
9
10
console.log(result); // 2021-09-11T09:12:05.051Z
In this example, we create a reusable function that returns last file modification date.
xxxxxxxxxx
1
const fs = require('fs');
2
3
const filePath = './file.txt';
4
5
const getLastModificationDate = (path) => {
6
const stats = fs.statSync(path);
7
return stats.mtime;
8
};
9
10
console.log(getLastModificationDate(filePath));
Example output:
xxxxxxxxxx
1
2021-09-11T09:12:05.051Z