Java Reference
In-Depth Information
the time in the United Kingdom, or even the time in Kuala Lumpur. You could, of course, say 15:30
EST and let your visitors work out what that means, but even that isn't foolproof. There is an EST in
Australia as well as in the United States. Wouldn't it be great if you could automatically convert the
time to the user's time zone? In this chapter, you see how.
In addition to looking at world time, you also look at how to create a timer in a web page. You'll
see that by using the timer you can trigger code, either at regular intervals or just once (for example,
five seconds after the page has loaded). You'll see how you can use timers to add a real‐time clock
to a web page. Timers can also be useful for creating animations or special effects in your web
applications, which you explore in later chapters.
World time
The concept of now means the same point in time everywhere in the world. However, when
that point in time is represented by numbers, those numbers differ depending on where you are.
What is needed is a standard number to represent that moment in time. This is achieved through
Coordinated Universal Time (UTC), which is an international basis of civil and scientific time and
was implemented in 1964. It was previously known as GMT (Greenwich Mean Time), and, indeed,
at 0:00 UTC it is midnight in Greenwich, London.
The following table shows local times around the world at 0:00 UTC time:
san FranCisCo
neW York (est)
GreenWiCh, london
Berlin, GermanY
tokYo, Japan
4:00 pm
7:00 pm
0:00 (midnight)
1:00 am
9:00 am
Note Note that the times given are winter times—no daylight savings hours
are taken into account.
The support for UTC in JavaScript comes from a number of methods of the Date object that are
similar to those you have already seen. For each of the set‐date and get‐date-type methods you've
seen so far, there is a UTC equivalent. For example, setHours() sets the local hour in a Date object,
and setUTCHours() does the same thing for UTC time. You look at these methods in more detail in
the next section.
In addition, three more methods of the Date object involve world time.
You have the methods toUTCString() and toLocaleString() , which return the date and time
stored in the Date object as a string based on either UTC or local time. Most modern browsers also
have these additional methods: toLocaleTimeString() , toTimeString() , toLocaleDateString() ,
and toDateString() .
If you simply want to find out the difference in minutes between the current locale's time and
UTC, you can use the getTimezoneOffset() method. If the time zone is behind UTC, such as in
the United States, it will return a positive number. If the time zone is ahead, such as in Australia or
Japan, it will return a negative number.
 
Search WWH ::




Custom Search