Node.js stats.birthtimeNs Property
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:
// Node.js program to demonstrate the // stats.birthtimeNs property // Accessing fs module const fs = require( 'fs' ); // Calling fs.Stats stats.birthtimeNs // using stat fs.stat( './' , { bigint: true }, (err, stats) => { if (err) throw err; // The timestamp when the file is created (in NS) console.log( "Using stat: " + stats.birthtimeNs); }); // Using lstat fs.lstat( './filename.txt' , { bigint: true }, (err, stats) => { if (err) throw err; // The timestamp when the file // is created (in NS) console.log( "Using lstat: " + stats.birthtimeNs); }); |
Output:
Using stat: 1589375311991945.3 Using lstat: 1592667100334387
Example 2:
// Node.js program to demonstrate the // stats.birthtimeNs property //accessing fs module const fs = require( 'fs' ).promises; // Calling stats.birthtimeNs property // from fs.Stats class (async () => { const stats = await fs.stat( './filename.txt' , { bigint: true }); // The timestamp when the file // is created (in NS) 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