Node.js — Get a File’s Created Date

The file system module in Node.js provides all file-related functions. Access file details with the help of a so called “stats” instance.

Node.js Series Overview

Created Date of a File

Use the fs module in Node.js to retrieve a stats instance of a given file including meta data like the creation date, last modified dates, accessed dates, and more.

The file’s created date is the birthtime property in the stats object:

const Fs = require('fs')

function createdDate (file) {  
  const { birthtime } = Fs.statSync(file)

  return birthtime
}

console.log(  
  createdDate('./file.txt')
)
// -> 2018-12-28T10:29:43.879Z

The birthtime is a JavaScript Date instance with all the date-related methods and properties.

Notice that there’s a birthtimeMs property in the stats object holding the file’s created date in milliseconds.

Enjoy coding & make it rock!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.