Node.js fs.utimesSync() Method
Last Updated :
08 Oct, 2021
Improve
The fs.utimesSync() method is used to synchronously 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.
Syntax:
javascript
Output:
javascript
Output:
fs.utimesSync( 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.
// Node.js program to demonstrate the
// fs.utimesSync() method
// Import the filesystem module
const fs = require('fs');
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 utimesSync() function to assign
// the new timestamps
fs.utimesSync("example_file.txt",
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);
Details before changing time: Modification Time: 2015-12-20T19:42:00.000Z Access Time: 2020-05-25T15:02:04.809Z Details after changing time: Changed Modification Time: 2020-05-25T15:09:37.412Z Changed Access Time: 2020-05-25T15:09:37.412ZExample 2:
// Node.js program to demonstrates the
// fs.utimesSync() method
// Import the filesystem module
const fs = require('fs');
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("December 21, 2015 01:12:00");
let changedAccessTime = new Date("December 23, 2015 01:15:00");
// Use the utimesSync() function to assign
// the new timestamps
fs.utimesSync("example_file.txt",
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);
Details before changing time: Modification Time: 2020-05-25T15:09:37.412Z Access Time: 2020-05-25T15:09:37.412Z Details after changing time: Changed Modification Time: 2015-12-20T19:42:00.000Z Changed Access Time: 2015-12-22T19:45:00.000ZReference: https://nodejs.org/api/fs.html#fs_fs_utimessync_path_atime_mtime