Learn how to use Node.js to get the last updated date of a file.
The fs
module in Node.js provides all the necessary file functions. One of these functions is statSync()
, which allows us to get file details synchronously.
To retrieve the last updated date of a file, simply call statSync()
and pass in the file path (either relative or absolute). It will return an object that contains the mtime
property.
The mtime
property is a Date
object instance that represents the last modified date of the file.
Here’s an example code snippet:
const fs = require('fs');
const getFileUpdatedDate = (path) => {
const stats = fs.statSync(path);
return stats.mtime;
}
If you need to work with the Date
object further, you can refer to our JavaScript Date guide.
Tags: Node.js, file handling, fs
module, last updated date