Home > Back-end >  Convert GMT 0000 Timestamp to proper time
Convert GMT 0000 Timestamp to proper time

Time:10-23

How do i get javascript to return the correct time format. In the code below the result of text2 shows up as 9:45:00 AM instead of 1:45:00 PM which is the correct time selected user selected in firebase. I need just the time

const d = new Date("Fri Oct 28 2022 13:45:00 GMT 0000 (Coordinated Universal Time)");
let text = d.toUTCString();
let text2 = d.toLocaleTimeString();
document.getElementById("demo").innerHTML = text2;

CodePudding user response:

You need to add your GMT X gaps to your calculation, by using the new Date().getTimezoneOffset()

let d = new Date("Fri Oct 28 2022 13:45:00 GMT 0000 (Coordinated Universal Time)");
d.setMinutes(d.getMinutes()   new Date().getTimezoneOffset())
let text = d.toUTCString();
let text2 = d.toLocaleTimeString();
document.getElementById("demo").innerHTML = text2;
<div id="demo"></div>

  • Related