Issue
My website currently displays UTC time like this:
2022-03-22T23:38:25.748Z
But I would like for it to be formatted like this:
23:39:22 UTC
Here is the javascript I have:
<script type="text/javascript">
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}
function display_ct() {
var x = new Date()
var x1=x.toISOString();// changing the display to UTC string
document.getElementById('ct').innerHTML = x1;
tt=display_c();
}
<body onload=display_ct();><span id='ct' >
Can you help me format this? I've looked into using angular and searched other methods for implementing this but it seems that there are many ways to do this. The things I've tried do not display the format correctly. The code above is the closest solution I have found.
Solution
new Date().toISOString().slice(11, 19) + ' UTC'
A nice and intentional property of the ISO datetime format is that most of the parts are always the same length.
Note that truncating, not rounding, is the correct behavior.
Answered By - twhb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.