Onjsdev

Share


How To Change File Timestamps (Access And Modification Time) In Nodejs


By onjsdev

Nov 5th, 2023

You can easily change the timestamps of a file using built-in node modules, fs (file system) and path. This capability allows you to modify the file's creation, access, and modification timestamps according to your requirements.

In this article, we'll walk through how to change these timestamps in a simple and straightforward way.

How To Change Timestamps Of A File

Before we start, let's understand timestamps:

A file in a computer system typically has three timestamps associated with it:

  • Creation Time: This timestamp indicates when the file was initially created.
  • Access Time: The access timestamp represents the last time the file was accessed or read.
  • Modification Time: The modification timestamp denotes the last time the file's content was modified.

To change the file timestamps in Nodejs, we have powerful file system method, fs.utimes() .This method allows you to set the access and modification timestamps of a file based on your desired values.

Here is the example of how to change timestamps of a text file in a directory we run the code. Please make sure you provide the correct path for the file whose timestamps you want to change.

const fs = require('fs');
const path = require('path');

const filePath = path.join(__dirname, 'test.txt');

// Specify the desired timestamp values
const accessTime = new Date('May 30, 2023 10:30:00 UTC');
const modificationTime = new Date('May 30, 2023 15:45:00 UTC');

// Change the file timestamps
fs.utimes(filePath, accessTime, modificationTime, (err) => {
  if (err) {
    console.error('An error occurred while changing the file timestamps:', err);
    return;
  }

  console.log('File timestamps changed successfully!');
});

Upon successful execution, the console will display the message "File timestamps changed successfully!"

How To Checks Timestamps Of File

After changing the timestamps of a file, you may need to check the timestamps of it. In Nodejs, you can use the fs.stat method provided by the fs module. This method retrieves information about the file, including the timestamps.

Here's an example code snippet that demonstrates how to check the timestamps of a file:

const fs = require('fs');
const path = require('path');

const filePath = path.join(__dirname, 'test.txt');

// Check the file timestamps
fs.stat(filePath, (err, stats) => {
  if (err) {
    console.error('An error occurred while checking the file timestamps:', err);
    return;
  }

  console.log('File timestamps:');
  console.log('Creation Time:', stats.birthtime);
  console.log('Access Time:', stats.atime);
  console.log('Modification Time:', stats.mtime);
});

Inside the callback function, we can access the file information using the stats object.

  • The stats.birthtime represents the creation time of the file,
  • stats.atime represents the access time, and
  • stats.mtime represents the modification time.

We log these timestamps to the console for display. You can verify the changes happened you can call this method before and after changing timestamps.

Conclusion

Changing file timestamps using Nodejs is a straightforward process thanks to the fs module. By using the fs.utimes() method, you can easily modify the creation, access, and modification timestamps of a file according to your needs.

Thank you for reading.