0

I am using the following code to display a last updated date on my webpage. The problem is the format is in MM/DD/YYYY but I need DD/MM/YYYY to be there.

In HEAD :

<script type="text/javascript">
onload = function()
{
document.getElementById("lastModified").innerHTML = "Last updated :  " + 
document.lastModified.split(" ")[0];
}
</script>

In body

<span id="lastModified"></span>
Itay Gal
  • 10,706
  • 6
  • 36
  • 75
mdhz
  • 113
  • 1
  • 10
  • Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Itay Gal Dec 09 '18 at 06:36

2 Answers2

0

You can covert document.lastModified to a Date object, then get all the relevant data for your date using the Date object's methods such as getDate(), getMonth() and getFullYear(). Then merge all these components into a string to form a full date.

See example below:

window.onload = function() {
  let lastMod = new Date(document.lastModified);
  let day = lastMod.getDate();
  let month = lastMod.getMonth();
  let year = lastMod.getFullYear();
  let lastModStr = day + '/' + month + '/' + year;
  document.getElementById("lastModified").textContent = "Last updated :  " + lastModStr;
}
<span id="lastModified"></span>
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

You can use toLocaleDateString() to format your date

onload = function() {
  document.getElementById("lastModified").innerHTML = "Last updated :  " +
    new Date(document.lastModified).toLocaleDateString('in');
}
<span id="lastModified"></span>
Md. Monirul Alom
  • 275
  • 4
  • 16