find the creation date of a directory in typescript

Here's a TypeScript function that uses the Node.js fs module to get the creation date of a directory:

index.ts
import fs from 'fs';

function getDirectoryCreationDate(directoryPath: string): Date | undefined {
  try {
    const stat = fs.statSync(directoryPath);
    return stat.birthtime;
  } catch (error) {
    console.error(`Error getting directory creation date: ${error}`);
    return undefined;
  }
}
297 chars
12 lines

To use this function, simply pass the path of the directory you want to get the creation date for as an argument:

index.ts
const directoryPath = '/path/to/my/directory';
const creationDate = getDirectoryCreationDate(directoryPath);

if (creationDate) {
  console.log(`The creation date of '${directoryPath}' is: ${creationDate}`);
} else {
  console.log(`Could not get creation date for '${directoryPath}'`);
}
288 chars
9 lines

gistlibby LogSnag