Node.js | fsPromises.utimes() Method

The fsPromises.utimes() method is used to asynchronously change the modification and access timestamps of a file. 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.

It changes the file system timestamps of the object referenced by path then resolves the Promise with no arguments upon success.

Syntax:

fsPromises.utimes( path, atime, mtime )

Parameters: This method accepts three parameters as mentioned above and described below:

  • path: It is a string that denotes the path 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.

The atime and mtime arguments follow these rules:



  1. Values can be either number representing Unix epoch time, Dates, or a numeric string like ‘123456789.0’.
  2. If the value can not be converted to a number, or is NaN, Infinity or -Infinity, an Error will be thrown.

Example: This example to illustrate the fsPromises.utimes() method in Node.js. Create a example_gfg.txt file for input.

Filename: index.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the 
// fsPromises.utimes() method 
  
// Import the filesystem module 
const fs = require('fs');
const fsPromises = require('fs').promises;
  
console.log("Details before changing time:");
  
// Get the stats object of the file 
prevStats = fs.statSync("example_gfg.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 newModifiedTime = new Date();
let newAccessTime = new Date();
  
// Use the utimes() function to assign 
// the new timestamps 
fsPromises.utimes("example_file.txt"
        newAccessTime, newModifiedTime);
  
// Get the stats object of the file 
console.log("\nDetails after changing time:");
  
// Get the stats object of the file 
changedStats = fs.statSync("example_gfg.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);

chevron_right


Step to run this program: Run index.js file using the following command:

node index.js

Output:

Details before changing time:
Modification Time: 2020-06-11T17:25:51.136Z
Access Time: 2020-06-11T16:50:51.223Z

Details after changing time:
Changed Modification Time: 2020-06-11T17:25:51.136Z
Changed Access Time: 2020-06-11T16:50:51.223Z

Reference: https://nodejs.org/api/fs.html#fs_fspromises_utimes_path_atime_mtime

My Personal Notes arrow_drop_up

Stepping steps of my ownself

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.