Node.js stats.birthtimeNs Property
Last Updated :
07 Oct, 2021
The stats.birthtimeNs property is an inbuilt application programming interface of the fs.Stats class is used to get the timestamp when the file is created since the POSIX epoch expressed in milliseconds.
Syntax:
stats.birthtimeNs;
Parameters: This property does not have any parameters, but during creation of stats object {bigint:true} must be passed as options.
Return Value: It returns a number or BigInt value which represents the timestamp when the file is created since the POSIX epoch expressed in milliseconds.
Below examples illustrate the use of stats.birthtimeNs property in Node.js:
Example 1:
const fs = require( 'fs' );
fs.stat( './' , { bigint: true }, (err, stats) => {
if (err) throw err;
console.log( "Using stat: " + stats.birthtimeNs);
});
fs.lstat( './filename.txt' ,
{ bigint: true }, (err, stats) => {
if (err) throw err;
console.log( "Using lstat: "
+ stats.birthtimeNs);
});
|
Output:
Using stat: 1589375311991945.3
Using lstat: 1592667100334387
Example 2:
const fs = require( 'fs' ).promises;
(async () => {
const stats = await fs.stat(
'./filename.txt' , { bigint: true });
console.log( "Using stat synchronous: "
+ stats.birthtimeNs);
})(). catch (console.error)
|
Output:
Using stat synchronous: 1592667100334387
Note: The above program will compile and run by using the node filename.js
command and use the file_path correctly.
Reference: https://nodejs.org/api/fs.html#fs_stats_birthtimens
Share your thoughts in the comments
Please Login to comment...