0

I am currently trying to translate a Wallpaper Engine wallpaper from Chinese to English, part of this process is converting the 24 hours time standard to the 12 hours standard. Here is the code I believe is used to calculate the time, any help would be most appreciated! If this is not the correct section of code or if more info is needed, let me know, I'd be happy to share more!

//update time
function upDateTime(){
  var date = new Date();
  var hour = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();
  if(hour < 10){hour = "0" + hour;} 
  if(minute < 10){minute = "0" + minute;}
  var times = document.getElementById("time").getElementsByTagName("p");
  times[0].innerText = hour
  times[1].innerText = minute;
  setTimeout(upDateTime, 60 - second);
  //update Day
  if(hour == 0 && upDay == true){
    upDay = false;
    pushDate();
    setTimeout(function(){
      upDay = true;
    },1000*3600*1.01);
  }
}
upDateTime();
#time{
  display: flex;
  gap: 2px;
  align-items: center;
}
<button id="time" onclick="upDateTime();">
  <p></p>
  :
  <p></p>
</button>

I attempted to read some similar Stack Overflow articles, but was unable to successfully get the time format to change due to the code not originally coming from me.

ATP
  • 2,939
  • 4
  • 13
  • 34
  • 1
    How about `if (hour > 12) { hour -= 12 }` instead of, or before, adding a leading zero? – Andrew Morton Jul 22 '23 at 19:59
  • You could use the [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) and get it converted correctly, with AM and PM, and the date in correct order. – some Jul 22 '23 at 22:41
  • Thanks @AndrewMorton that line helped out a bunch! – Nyx Schaefer Jul 23 '23 at 03:04

0 Answers0