0

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.

NA NA
  • 1
  • Does this answer your question? [Update the "last modified date" of a file](https://stackoverflow.com/questions/59921345/update-the-last-modified-date-of-a-file) – mfdebian Apr 04 '23 at 19:02
  • @mfdebian it *seems* like this question is about browser JavaScript, not Node. – Pointy Apr 04 '23 at 19:09
  • @Pointy u're right, my bad. – mfdebian Apr 04 '23 at 19:16
  • Yes, just regular JS, and also I want to set a custom date instead of the current date, so it could be a week ago, month, year ago, etc. – NA NA Apr 04 '23 at 19:16

1 Answers1

0

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
mfdebian
  • 108
  • 1
  • 10
  • Oh yes, I forgot to mention, the code I used in my original post also shows the correct date when I check the console, but when I download the image it still shows the current date. Is it an issue on my end? Or is it possible the browser overrides/doesn't allow changing the last modified date? – NA NA Apr 04 '23 at 19:22
  • Maybe there's something that I'm not understanding, but when you _download_ a file to your computer, you're _writing_ that file in your computer's disk, so it's logical that the _created_ or _modified_ date of the file matches the one when it was written. Anyone else please correct me if I'm wrong. – mfdebian Apr 04 '23 at 19:31
  • Yea, I guess my use case is a little weird which is why I could not find info on this. I had a lot of old images that I lost, but I can find them online which contains the original date posted. So I want to use that date as the last modified date so I know the original date. I am currently trying to do this through a chrome extension, I guess its not possible. I believe you can edit the last modified date manually with some Windows app, but it will be a lot more manual labor. – NA NA Apr 04 '23 at 19:46