Display and Formatting UTC Time On Static Website in Real-Time

All we need is an easy explanation of the problem, so here it is.

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.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

This should produce the format you want to have.

let date = new Date();
let utc_string = date.toUTCString().match(/..:..:.. .*/)[0];
console.log(utc_string);

Method 2

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.

Method 3

Use the Intl.DateTimeFormat method for this as follows:

  function display_c(){
    const refresh = 1000; 
    mytime = setTimeout('display_ct()',refresh)
  }
  
  function display_ct() {
    let dtf = new Intl.DateTimeFormat("en-GB", { timeZone: "GMT", hour12: false, timeStyle: "long" });
    document.getElementById('ct').innerHTML = dtf.format( new Date() );
    tt = display_c();
   }

display_ct();
<div id="ct"></div>

Customization:

You can either use the various configuration options available to customize the format of the datetime, or use the following setup for displaying a custom String:

  function display_ct() {
    let dtf = new Intl.DateTimeFormat("en-GB", { timeZone: "GMT", hour12: false, timeStyle: "medium" });
    document.getElementById('ct').innerHTML = dtf.format( new Date() ) + " Zulu";
   }

Available configuration options: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat

Method 4

I found a web to helping you. And i give an example to you too.
https://www.codevscolor.com/javascript-iso-utc

const date = new Date('2022-03-22T23:38:25.748Z')

console.log(date.toString())
console.log(date.toUTCString())

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply