how to use the utimes function from fs-extra in javascript

To use the utimes function from fs-extra in Node.js, you first need to install the module:

index.tsx
npm install fs-extra
21 chars
2 lines

Then, you can require the module and use the utimes function to modify the access and modification times of a file:

index.tsx
const fse = require('fs-extra');

const filePath = '/path/to/file';
const accessTime = new Date('2021-01-01T00:00:00Z');
const modifyTime = new Date('2021-01-02T00:00:00Z');

// Set the access and modification times of the file
fse.utimes(filePath, accessTime, modifyTime, (err) => {
  if (err) throw err;
  console.log('File access and modification times have been changed successfully.');
});
395 chars
12 lines

In the example above, we specify the filePath of the file we want to modify and the accessTime and modifyTime with Date objects. Then, we call the utimes function and provide a callback function that will be called once the operation is finished. If an error occurs, it will be thrown. Otherwise, a success message will be printed to the console.

gistlibby LogSnag