0

In my website I am printing the date the html file was last editied at the end of each page, using this string:

Modified: ' + document.lastModified

The trouble with that is that it uses American date order m/d/yyyy. That is fine for people viewing the page in the US, but I want it to reflect the settings for the user's machine.

Is there a simple solution from the standard JS library?

MarkAurelius
  • 1,203
  • 1
  • 14
  • 27
  • There is the [toLocaleDateString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString) method – Kei Sep 30 '19 at 23:20
  • Possible duplicate of [Display date/time in user's locale format and time offset](https://stackoverflow.com/questions/85116/display-date-time-in-users-locale-format-and-time-offset) – Cyril F Sep 30 '19 at 23:24

1 Answers1

0

Let your lastModified be a timestamp. Then you can do this:

document.lastModified = 1569885129000 // timestamp 
let dateObj = new Date(document.lastModified);
let str = 'Modified: ' + dateObj.toLocaleDateString();

You can read more about Date object here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

fiter
  • 743
  • 1
  • 12
  • 25
  • That is not guaranteed to present the date in a "locale aware" format. Many browsers in particular default to US format, and most support only a few common formats. – RobG Oct 01 '19 at 02:40