Node.js fs.futimes() Method
The fs.futimes() method is used to asynchronously change the modification and access timestamps of given file descriptor. The timestamps can be specified using a number, string, or Date object. An error would be thrown if the timestamp cannot be converted to a proper number, or is NaN, Infinity or -Infinity.
Syntax:
fs.futimes( fd, atime, mtime, callback )
Parameters: This method accepts four parameters as mentioned above and described below:
- fd: It is an integer that denotes the file descriptor of the file whose timestamps have to be changed.
- atime: It is number, string or Date object that denotes the new access timestamp to be set.
- mtime: It is number, string or Date object that denotes the new modification timestamp to be set.
- callback: It is a function that would be called when the method is executed.
- err: It is an error that would be thrown if the method fails.
Below examples illustrate the fs.futimes() method in Node.js:
Example 1:
// Node.js program to demonstrate the // fs.futimes() method // Import the filesystem module const fs = require( 'fs' ); // Get the file descriptor of the file const fd = fs.openSync( "example_file.txt" , "r+" ); console.log( "Details before changing time:" ); // Get the stats object of the file prevStats = fs.statSync( "example_file.txt" ); // Access the modified and access time of the file console.log( "Modification Time:" , prevStats.mtime); console.log( "Access Time:" , prevStats.atime); // Get the current time to change the timestamps let changedModifiedTime = new Date(); let changedAccessTime = new Date(); // Use the futimes() function to assign // the new timestamps to the file descriptor fs.futimes(fd, changedAccessTime, changedModifiedTime, () => { // Get the stats object of the file console.log( "\nDetails after changing time:" ); // Get the stats object of the file changedStats = fs.statSync( "example_file.txt" ); // Access the changed modified and access time of the file console.log( "Changed Modification Time:" , changedStats.mtime); console.log( "Changed Access Time:" , changedStats.atime); }); |
Output:
Details before changing time: Modification Time: 2020-05-25T15:59:10.807Z Access Time: 2020-05-25T15:59:10.807Z Details after changing time: Changed Modification Time: 2020-05-25T16:01:34.915Z Changed Access Time: 2020-05-25T16:01:34.915Z
Example 2:
// Node.js program to demonstrate the // fs.futimes() method // Import the filesystem module const fs = require( 'fs' ); // Get the file descriptor of the file const fd = fs.openSync( "example_file.txt" , "r+" ); console.log( "Details before changing time:" ); // Get the stats object of the file prevStats = fs.statSync( "example_file.txt" ); // Access the modified and access time of the file console.log( "Modification Time:" , prevStats.mtime); console.log( "Access Time:" , prevStats.atime); // Get the current time to change the timestamps let changedModifiedTime = new Date( "April 01, 2019 04:13:20" ); let changedAccessTime = new Date( "April 12, 2019 08:12:40" ); // Use the futimes() function to assign // the new timestamps to the file descriptor fs.futimes(fd, changedAccessTime, changedModifiedTime, () => { // Get the stats object of the file console.log( "\nDetails after changing time:" ); // Get the stats object of the file changedStats = fs.statSync( "example_file.txt" ); // Access the changed modified and access time of the file console.log( "Changed Modification Time:" , changedStats.mtime); console.log( "Changed Access Time:" , changedStats.atime); }); |
Output:
Details before changing time: Modification Time: 2020-05-25T16:06:28.977Z Access Time: 2020-05-25T16:06:28.977Z Details after changing time: Changed Modification Time: 2019-03-31T22:43:20.000Z Changed Access Time: 2019-04-12T02:42:40.000Z
Reference: https://nodejs.org/api/fs.html#fs_fs_futimes_fd_atime_mtime_callback