find the last modified date of a directory in typescript

To find the last modified date of a directory using TypeScript, we need to use the built-in Node.js fs module which provides an interface to interact with the file system.

First, we need to import the fs module in our TypeScript file:

index.ts
import * as fs from 'fs';
26 chars
2 lines

Then we can use the statSync method to get the status of the directory, which includes the mtime property representing the last modified date.

Here's the code to find the last modified date of a directory in TypeScript:

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

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

const directoryStat = fs.statSync(directoryPath);
const lastModifiedDate = directoryStat.mtime;

console.log(`Last modified date of ${directoryPath}: ${lastModifiedDate}`);
245 chars
9 lines

This code will log the last modified date of the directory to the console in the format of a Date object. You can format the date object using toLocaleString() or other methods to make it more human-readable.

Note that statSync is a synchronous method, meaning it blocks the execution of the current thread until the operation is complete. If you want to asynchronously get the last modified date, you can use the stat method instead and handle the result using a callback or a promise.

gistlibby LogSnag