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.
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
birthtimeMs
and birthtime
properties of a Stats
object won't reflect the creation date in any POSIX system? if so, what value is returned for those properties? –
Snippy 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
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
birthtimeMs
and birthtime
properties of a Stats
object won't reflect the creation date in any POSIX system? if so, what value is returned for those properties? –
Snippy 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.
ctime
was for creation time
). I'll try some stuff, though, I'm gonna delete this if I won't find answer. –
Generative 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);
I am using Fedora and there is no 'birthtime' field in the fs.stat result. But maybe you can try using node's child_process to invoke ls --full-time. At least on my machine it's giving me correct result.
fs.stat result:
{ dev: 64771,
mode: 33279,
nlink: 1,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: 2098445,
size: 48523700,
blocks: 94776,
atime: Sat Jul 04 2015 19:01:29 GMT+1000 (AEST),
mtime: Thu Aug 22 2013 16:45:10 GMT+1000 (AEST),
ctime: Sat Jul 04 2015 19:01:29 GMT+1000 (AEST) }
ls --full-time
-rwxrwxrwx. 1 pahuang pahuang 22M 2013-06-23 17:51:08.000000000 +1000 test_media/processed/20130621.mkv
node 18, typescript, dayjs:
import fs from 'fs';
import dayjs from 'dayjs';
const stats: fs.Stats = fs.statSync($filename);
const birthtime: string = dayjs(stats.birthtime).format('YYYY-MM-DD HH:mm');
© 2022 - 2024 — McMap. All rights reserved.
util.inspect(stats)
should give you the information... What do you get as output of this? – Higherup