Javascript set lastModified date of image file

468 Views Asked by At

For example using the code found here: https://stackoverflow.com/a/62189458 I tried to change to this:

const file = new File([blob], fileName, { type: contentType, lastModified: 5000000 })

Looking at the image properties on explorer I still see the date modified is the same as the download date.

Is it possible to set the last modified date of an image file with JS? I think most people just want to read this data, but I actually want to set a custom last modified date. I searched for a long time, but did not find any solution, thanks.

1

There are 1 best solutions below

3
mfdebian On

Per the docs:

the last modified time can be supplied in the new File() constructor function. If it is missing, lastModified inherits the current time from Date.now() at the moment the File object gets created.

So you can do something like:

const fileWithDate = new File([], "file.bin", {
  lastModified: new Date(2017, 1, 1),
});
console.log(fileWithDate.lastModified); // returns 1485903600000

const fileWithoutDate = new File([], "file.bin");
console.log(fileWithoutDate.lastModified); // returns current time