find the last accessed date of a directory in typescript

To find the last accessed date of a directory in TypeScript, you can make use of the Node.js fs module, which provides an API for interacting with the file system.

Here's a code snippet that demonstrates how to get the last accessed date of a directory:

index.ts
import * as fs from 'fs';

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

try {
  const stats = fs.statSync(directoryPath);
  const lastAccessedTime = stats.atime;
  console.log(`Last accessed time: ${lastAccessedTime}`);
} catch (err) {
  console.error(err);
}
260 chars
12 lines

In the code above, we use the fs.statSync() method to get information about the file at the given path. This method returns a Stats object that contains information about the file, including the last accessed time (atime). We then simply log the last accessed time to the console.

Note that the fs.statSync() method throws an error if the file at the given path does not exist, so we wrap the call in a try-catch block to handle this case.

gistlibby LogSnag