find the last accessed date of a file in javascript

You can use the fs.stat() method provided by the built-in fs module in Node.js to get the file statistics, including the last accessed timestamp. Here's an example:

const fs = require('fs');

fs.stat('path/to/file', (err, stats) => {
  if (err) throw err;
  
  console.log(`Last accessed: ${stats.atime}`);
});
146 chars
8 lines

In the above code, replace path/to/file with the actual file path. The fs.stat() method takes a callback function that receives an error object (if any) and a stats object.

The stats object contains various file statistics, including the last accessed timestamp, which is returned by the stats.atime property.

You can format the timestamp using the Date() constructor, like this:

console.log(`Last accessed: ${new Date(stats.atime)}`);
56 chars
2 lines

This will output the timestamp in a human-readable format.

gistlibby LogSnag