find the last modified date of a file in javascript

You can find the last modified date of a file in JavaScript by using the statSync method from the fs module. This method returns an object containing information about the specified file, including its last modified time or mtime.

Here's an example code snippet that shows how to use statSync to find the last modified date of a file:

index.tsx
const fs = require('fs');

const filePath = 'path/to/your/file.txt';
const fileStats = fs.statSync(filePath);

const modDate = fileStats.mtime;
console.log(modDate);
166 chars
8 lines

In this code, we require the fs module and specify the file path we want to get information about. We then call fs.statSync() with the file path to get the file stats object. Finally, we extract the mtime property from the file stats object, which contains the last modified date of the file.

Note that statSync is a synchronous method, which means it will block the main thread until it is complete. If you need to get file information asynchronously, you can use the fs.stat() method instead.

gistlibby LogSnag