UTC functions : dates and times in a timezone-independent formats

UTC (Coordinated Universal Time) is the primary time standard by which the world regulates clocks and time. It is the same worldwide and does not change with the seasons (i.e., no daylight saving time). UTC is often referred to as "Zulu time" (Z) in aviation and military contexts.

getUTCHours()

We know how to get local computer ( client computer ) time by using getHours function. However to get the UTC time or GMT we have to use getUTCHours function. This function uses the local time setting and calculates the corresponding GMT time. We can check the difference between the two but before that here is the syntax with an example.
<script type="text/javascript"> 
var dt= new Date();
document.write("using getUTCHours : ");
document.write(dt.getUTCHours());
</script>
We can compare the output using getHours() and getUTCHours()

<script type="text/javascript"> 
var dt= new Date();
document.write("using getHours : ");
document.write(dt.getHours()); 
</script>
<br><br>
<script type="text/javascript"> 
var dt= new Date();
document.write("using getUTCHours : ");
document.write(dt.getUTCHours()); 
</script>
Here is the output of above code.


getUTCMinutes()

By using getUTCMinutes() function we can read minutes part of the UTC time. Here is a sample code.
<script type="text/javascript">
var dt= new Date();
document.write("using getutcMinutes : ");
document.write(dt.getUTCMinutes()); 
</script>
The output is here


getTimezoneOffset()

Return Value: The method returns the difference in minutes between the local time and UTC. The value is positive if the local time zone is behind UTC, and negative if it is ahead of UTC.

Daylight Saving Time: The value returned by getTimezoneOffset() can vary throughout the year if the local time zone observes daylight saving time.
<script type="text/javascript"> 
var dt= new Date();
document.write(dt.getTimezoneOffset());
</script>
The output is here

toUTCString()

Some time we have to convert date object to universal time standard. The output of such conversion will be a string. We can use toUTCString to convert date object to string format. Here is a sample code

Here is the code to do this .
<script type="text/javascript"> 
var dt= new Date();
document.write(dt);
document.write("<br>after conversion <br>");
document.write(dt.toUTCString());
</script>
While storing cookies we have to store expire date of the data in this format.

Date.UTC()

const utcDate = new Date(Date.UTC(2024, 7, 28, 12, 0, 0));

getUTCDate()

const day = utcDate.getUTCDate();

getUTCDay()

const weekday = utcDate.getUTCDay();

getUTCFullYear()

const year = utcDate.getUTCFullYear();

getUTCSeconds()

const seconds = utcDate.getUTCSeconds();

getUTCMilliseconds()

const milliseconds = utcDate.getUTCMilliseconds();

getUTCMonth()

const month = utcDate.getUTCMonth();

getUTCFullYear()

const fullYear = utcDate.getUTCFullYear();

Use Case

These UTC functions are especially useful when you need to handle or display times consistently across different time zones, as they allow you to work with a standard time reference that does not change based on the user's location or the system's timezone settings.
Date Reference
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer