Introduction to UTC Dates
Most JavaScript Date methods have an equivalent method that deals with UTC time rather than local time. For instance, getHours() returns hour in local time, while getUTCHours() returns the hour in UTC time. You can get a Date object in human readable form relative to local time using toLocaleString() and relative to UTC using toUTCString(). The JavaScript Date object even has a method named UTC. The UTC() method is a static method used to convert a date in a specific format to milliseconds since January 1, 1970 (relative to UTC) . You can then use the Date() constructor to create a Date object with this value.
For example, China has announced that the opening ceremonies of the 2008 Olympics will be on August 8, 2008 at 8:08pm. We can use JavaScript's UTC() method to find out when this is in local time.
var offset=-8; //offset from UTC
var luckyday=new Date(Date.UTC(2008,7,8,(20 + offset ),8);
document.write(" The 2008 Olympics starts at " + luckyday.toUTCString());
document.write(" In local time this is " + luckyday.toLocaleString())
Note: In the example above, I converted from Beijing time to UTC by adding the offset from UTC ("-8") to the Beijing time - this is why argument 4 of Date.UTC reads "(20 + offset)" above.
If you have JavaScript available in your browser, you can see a live example of this code here.
REFERENCE
Date.UTC( year, month[, day, [hour, [minutes, [seconds, [milliseconds]]]]])
converts the given date into milliseconds since January 1, 1970.
Note: all arguments are digits. As always in JavaScript, a month of 0 is January. If any argument other than year is a larger or smaller number than than possible for the value it represents, it is used to affect the next larger argument – for instance, a month of 13 is interpreted as February of the next year and a month of -2 is interpreted as November of the previous year.
SOURCE
U.S. Naval Observatory. "What is Universal Time?." 30 October 2003. U.S. Naval Observatory, Astronomical Applications Department. 6 July 2007. <https://aa.usno.navy.mil/faq/docs/UT.html>
This site needs an editor - click to learn more!
You Should Also Read:
Introduction To The JavaScript Date Object
Getting Dates and Parts of Dates in JavaScript
JavaScript/Java Site and Newsletter
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map
Content copyright © 2023 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact
BellaOnline Administration
for details.