Reputation: 21
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;
Upvotes: 1
Views: 485
Reputation: 1021
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>
Upvotes: 1