How to find out when a file was created in Node.js

Published Saturday, January 16, 2021

Jonas Arnklint
@arnklint

Knowing when a file was created from looking at its meta information or stat can be handy whenever you are want to automate removing files or using logic that only makes a program create a file if another file was not since a particular date.

Node.js fs module contains a function that can be used to retreive file meta information. It´s called stat and returns this object, containing the ctime property which indicates when the file was created:

{
  dev: 2269,
  mode: 33188,
  nlink: 1,
  uid: 1000,
  gid: 1000,
  rdev: 0,
  blksize: 4096,
  ino: 271,
  size: 0,
  blocks: 0,
  atimeMs: 1582871562365.894,
  mtimeMs: 1582871556897.5554,
  ctimeMs: 1582871556897.5554,
  birthtimeMs: 1582871556897.5554,
  atime: 2021-01-28T06:32:42.366Z,
  mtime: 2021-01-28T06:32:36.898Z,
  ctime: 2021-01-28T06:32:36.898Z,
  birthtime: 2021-01-28T06:32:36.898Z
}

Using a helper funcion to solve this can be a good practice if you need it in multiple places.

this is my code 412

You can also use the fs.stat function to retrieve the date when a file was created or detecting file size.