Node.js fs.futimesSync() Method
The fs.futimesSync() method is used to synchronously 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.futimesSync( fd, atime, mtime )
Parameters: This method accepts three parameters as mentioned above and described below:
- fd: It is an integer value 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.
Below examples illustrate the fs.futimesSync() method in Node.js:
Example 1:
// Node.js program to demonstrate the// fs.futimesSync() method // Import the filesystem moduleconst fs = require('fs'); // Get the file descriptor of the fileconst fd = fs.openSync("example_file.txt", "r+"); console.log("Details before changing time:"); // Get the stats object of the fileprevStats = fs.statSync("example_file.txt"); // Access the modified and access time of the fileconsole.log("Modification Time:", prevStats.mtime);console.log("Access Time:", prevStats.atime); // Get the current time to change the timestampslet changedModifiedTime = new Date();let changedAccessTime = new Date(); // Use the futimesSync() function to assign// the new timestamps to the file descriptorfs.futimesSync(fd, changedAccessTime, changedModifiedTime); // Get the stats object of the fileconsole.log("\nDetails after changing time:"); // Get the stats object of the filechangedStats = fs.statSync("example_file.txt"); // Access the changed modified and access time of the fileconsole.log("Changed Modification Time:", changedStats.mtime);console.log("Changed Access Time:", changedStats.atime); |
Output:
Details before changing time: Modification Time: 2015-12-20T19:42:00.000Z Access Time: 2020-05-25T15:40:13.508Z Details after changing time: Changed Modification Time: 2020-05-25T15:52:21.546Z Changed Access Time: 2020-05-25T15:52:21.546Z
Example 2:
// Node.js program to demonstrate the// fs.futimesSync() method // Import the filesystem moduleconst fs = require('fs'); // Get the file descriptor of the fileconst fd = fs.openSync("example_file.txt", "r+"); console.log("Details before changing time:"); // Get the stats object of the fileprevStats = fs.statSync("example_file.txt"); // Access the modified and access time of the fileconsole.log("Modification Time:", prevStats.mtime);console.log("Access Time:", prevStats.atime); // Get the current time to change the timestampslet changedModifiedTime = new Date("March 16, 2019 04:12:00");let changedAccessTime = new Date("December 18, 2019 08:15:00"); // Use the futimesSync() function to assign// the new timestamps to the file descriptorfs.futimesSync(fd, changedAccessTime, changedModifiedTime); // Get the stats object of the fileconsole.log("\nDetails after changing time:"); // Get the stats object of the filechangedStats = fs.statSync("example_file.txt"); // Access the changed modified and access time of the fileconsole.log("Changed Modification Time:", changedStats.mtime);console.log("Changed Access Time:", changedStats.atime); |
Output:
Details before changing time: Modification Time: 2020-05-25T15:52:21.546Z Access Time: 2020-05-25T15:52:21.604Z Details after changing time: Changed Modification Time: 2019-03-15T22:42:00.000Z Changed Access Time: 2019-12-18T02:45:00.000Z
Reference: https://nodejs.org/api/fs.html#fs_fs_futimessync_fd_atime_mtime


.png)
