Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file created date in node

In node I can get a file's modified date using the standard file stat object. Is there a way I can also find its created date? I can't find anything in the http://nodejs.org/api/fs.html#fs_class_fs_stats documentation at all.

like image 272
wheresrhys Avatar asked Jul 09 '13 15:07

wheresrhys


4 Answers

Here's a solution that worked well for me on both Linux and macOS (sorry Windows users).

It's a module you can import into your other code that uses Node's util.exec() method to pass a UNIX command, as a child process, and returns a UNIX timestamp string converted into an integer.

It will return null if the child process fails:

const util = require("util");
const exec = util.promisify(require("child_process").exec);

const executeCommand = async (cmd) => {
  try {
    return await exec(cmd, { timeout: 2000 }).then(async ({ stdout, stderr }) => {
      if (stderr) {
        return null;
      }
      if (stdout) {
        return stdout;
      }
    });
  } catch (cmdErr) {
    return null;
  }
};

exports.getFileDate = async (filePath) => {
  try {
    let cmd = ``;
    if (process.platform === "linux") {
      cmd = `stat -c %Y "${filePath}"`;
    } else if (process.platform === "darwin") {
      cmd = `stat -s "${filePath}"`;
    } else {
      console.error(`getFileDate() => Error: only 'linux' and 'darwin' platforms are supported`);
      return null;
    }

    let getDateResult = await executeCommand(cmd);
    if (getDateResult === null) {
      return null;
    }

    // Linux
    if (process.platform === "linux") {
      getDateResult = parseInt(getDateResult);
      return getDateResult;
    }

    // macOS
    else if (process.platform === "darwin") {
      // get the index where creation time starts
      let start = getDateResult.indexOf("st_ctime");

      // different timestamps are delimited by spaces
      let creationDate = getDateResult.substring(start, getDateResult.length);

      // parse the complete string to get 'st_ctime' value
      let splitResult = creationDate.split(" ");
      let timestamp = splitResult[0].replace("st_ctime=", "");

      // return 'null' if it's not a number
      if (isNaN(timestamp)) {
        return null;
      } else {
        timestamp = parseInt(timestamp);
        return timestamp;
      }
    }

    // catch errors
  } catch (err) {
    console.error(`getFileDate() => ${err}`);
    return null;
  }
};

Just import this new module into another script like so (assuming it's in the same directory):

const { getFileDate } = require("./getFileDate");

..and then you can pass a file path to the function call, and convert the UNIX timestamp into a readable date string like this:

let unixTimestamp = await getFileDate("path/to/some/file.txt");
let dateStr = new Date(unixTimestamp * 1000);
console.log(dateStr);
like image 194
Benji A. Avatar answered Oct 18 '22 02:10

Benji A.


If someone stumbles over this after all this time, as of Node v0.12.0 use this:

fs.stat(path, callback)

Where callback has two args err & stats. Stats object has property

birthtime

which is creation date.

Link to node api documentation link

like image 40
flaky Avatar answered Oct 18 '22 03:10

flaky


Whether or not you can get the file creation time depends on the OS and file system. Traditional POSIX only defines ctime, which is (rather confusingly), the inode modification date, not the creation date, as other people have mentioned. However, on some operating systems, you can get st_birthtimespec, or st_birthtime which is a true "create" time. You'll need to check sys/stat.h on your host operating system to see what, if anything, is available.

Unfortunately, whether or not you can access the entire stat structure from node.js is a different kettle of fish. But at least you can figure out if your OS even supports it and go from there.

2019 Update:

You can always access this property, but on many file systems it will be wrong. According to the Node.js docs:

On filesystems where birthtime is not available, this field may instead hold either the ctime or 1970-01-01T00:00Z (ie, Unix epoch timestamp 0). This value may be greater than atime or mtime in this case. On Darwin and other FreeBSD variants, also set if the atime is explicitly set to an earlier value than the current birthtime using the utimes(2) system call. https://nodejs.org/api/fs.html#fs_stats_birthtimems

like image 6
aleatha Avatar answered Oct 18 '22 03:10

aleatha


If you are using Linux then this information is not accessible (though it is stored in Ext4 file system). Thus fs.stat only returns atime, ctime, mtime values.

like image 2
Zbigniew Avatar answered Oct 18 '22 01:10

Zbigniew