Open In App

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:

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


Article Tags :