find the last modified date of a file in typescript

To find the last modified date of a file in TypeScript, you can use the fs module and its statSync method to get the file's status. Here's an example code snippet:

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

const filePath = 'path/to/file.txt';

const stats = fs.statSync(filePath);
const lastModified = stats.mtime;

console.log(`Last modified date: ${lastModified.toDateString()}`);
204 chars
9 lines

In this example, we import the fs module and specify the path to the file we want to check. We then get the file's status using fs.statSync() and store it in a variable called stats. mtime (short for "modification time") is a property of the stats object that contains the date and time when the file was last modified. We store this value in a variable called lastModified, which is a Date object. Finally, we display the last modified date using the toDateString() method of the Date object.

gistlibby LogSnag