find the last modified date of a directory in javascript

To find the last modified date of a directory in Node.js, you can use the file system (fs) module. The fs module provides a stat() method which returns an object with information about a file or directory.

Here's how you can use the fs.stat() method to get the last modified date of a directory:

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

const path = '/path/to/directory';

fs.stat(path, (err, stats) => {
  if (err) {
    console.error(err);
    return;
  }

  const lastModified = stats.mtime;
  console.log(lastModified);
});
218 chars
14 lines

In the above code, we first require the fs module. Then, we define the path of the directory whose last modified date we want to find.

Next, we call the fs.stat() method and pass in the path of the directory and a callback function as arguments. The callback function is called when the fs.stat() method completes.

If there is an error, we log it to the console and return. Otherwise, we access the mtime property of the stats object returned by fs.stat() and store it in the lastModified variable.

Finally, we log the lastModified variable to the console. The mtime property stores the last modified date of the directory.

gistlibby LogSnag