# Post 2022-03

### How to obtain global time and build the world clock

In this blog, we will show how to obtain the global time (Greenwich Mean Time) and how to set up the time for the world clock. Thanks for the post from "楼教主".

The demo below shows the webclocks with the exact time of the most popular regions in the world, and both summer time (EDT, PDT, CEST, AEST) and winter time (EST, PST, CET, AEDT) are considered.

Here is the demo: https://eulz.net/page/webclock.html

###### How to get GMT time:
1. getTimezoneOffset( ) returns the shifted time S between the current time C and GMT: S = GMT - C.
2. For example, if we are in EST time zone, our time is GMT-0400, we should add 4*60 =240 mins to EST in order to obtain GMT.
3. GMT = current time + getTimezoneOffset( ).
4. If we are in New York (GMT -0400), we can set the time below: EST = GMT - 4 hours.
5. If we are in Beijing (GMT +0800), we can set the time below: Beijing Time = GMT + 8 hours.

##### 1. Obtaining the time of various locations by your local time:
###### (1) Calculate the time gap between your local time and GMT time:
• ```    var dt = new Date;
console.log( dt.getTimezoneOffset() ); // -480
```
###### (2) Compute the exact time of your target location:

The final codes are shown below and you can see the effect here or click the figure on the top.

• ```    var dt = new Date;
console.log( dt.getTimezoneOffset() ); // -480
dt.setMinutes( dt.getMinutes() + dt.getTimezoneOffset() ); // current time(mins) + time zone shift(mins)
var dt_hour = -4;
var dt_min  = 0;
function iniTime() {
var now = new Date();
mincr = dt.getMinutes() + dt_min;
hincr = dt.getHours() + dt_hour;
console.log( "GMT format: ", now.toLocaleString());
console.log( "Local time format: ", now);
}
iniTime();
```
##### 2. How to find the winter time:

Some contries are still using winter time zone.

In the U.S., daylight saving time starts on the second Sunday in March and ends on the first Sunday in November, with the time changes taking place at 2:00 a.m. local time.

For the year 2023, Sunday, March 12, 2023, 2:00:00 AM clocks were turned forward 1 hour to Sunday, March 12, 2023, 3:00:00 AM local daylight time instead.

When local daylight time is about to reach Sunday, November 5, 2023, 2:00:00 AM clocks are turned backward 1 hour to Sunday, November 5, 2023, 1:00:00 AM local standard time instead.

Let us take New York as an example, its time is GMT-4 most of the year but GMT-5 in winter time. In 2023, its winter time vanished in Sun, March 12 2023 and will start in Sun November 6, 2022.

Thus, the winter time ends in one of the days among March 8 to 14, and starts in one of the days among November 1 to 7 every year. Then, we only need to determine the end date and the start date in each year among 03/08 to 03/14 and 11/01 to 11/07, respectively.

###### The codes are shown below:
• ```    var dt  = new Date;
// console.log( dt.getTimezoneOffset() ); // -480
dt.setMinutes( dt.getMinutes() + dt.getTimezoneOffset() ); // 当前时间(分钟) + 时区偏移(分钟)

var dt_hour = -4;
var dt_min  = 0;

dt.setMinutes( dt.getMinutes() + dt_hour*60 + dt_min); // calculate local time

var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var month_now=dt.getMonth()+1;
var ddy = days[dt.getDay()];
var ddt = dt.getDate();                        // get day
var ddh = dt.getHours();                       // get hour
var yy =dt.getDay();                           // get date of Sunday to Satursday
var winter = 0;

if (month_now >3 && month_now <11){winter = 0;}
else if (month_now == 3){
if (ddt >= 8 && ddt <= 14){
if(ddt-7 < yy+1){ winter = 1;}
else{
if (yy==0){if (ddh<2){ winter = 1;}else{winter =0;} }
else{winter = 0;}}}
else if (ddt < 8){ winter = 1;}
else{winter = 0;}}
else if (month_now == 11){
if (ddt >= 1 && ddt <= 7){
if(ddt < yy+1){ winter = 0;}
else{
if (yy==0){if (ddh<2){winter = 0;}else{winter =1;} }
else{winter = 1;}}}
else{winter = 1;}}
else{
winter = 1;
}

dt.setMinutes( dt.getMinutes() - winter*60 + dt_min); // calculate winter time
```

This article was updated by Liang on 07/10/2023. Thanks for your visiting and the demo is shown here: https://eulz.net/page/webclock.html