Detail JavaScript UTC time conversion method

  • 2020-11-20 06:01:22
  • OfStack

1. Introduction
1. UTC: Universal Time Coordinated, Coordinated Universal Time.
2. Greenwich Standard time (GMT)
Greenwich Mean time (Greenwich Mean Time, GMT) is the standard time at the Royal Greenwich Observatory on the outskirts of London, as the prime meridian is defined as the meridian that passes there. In theory, midday in Greenwich Mean time is the time when the sun crosses the Greenwich meridian. Due to the uneven speed of the Earth in its elliptical orbit, this time may be 16 minutes away from the actual solar time. The earth's daily rotation is somewhat irregular and is slowly slowing down. As a result, Greenwich time is no longer used as standard time. The current standard time, coordinated Universal Time (UTC), is provided by atomic clocks. Since February 5, 1924, Greenwich Observatory has been sending out timing messages every hour around the world. UTC is based on the exact timing provided by the standard GMT.
GMT (Greenwich Mean Time) -- Greenwich Mean time, the standard time of the British Empire in the mid-19th century, is the DE facto world standard time. It was mainly for the service of the railway system after 1840. It divides the world into 24 time zones based on the longitude of the Greenwich Observatory at zero degrees, and its position remains unchanged, except that it has been influenced by xenophobia, nationalism and certain anti-British sentiments at certain times.

The difference between GMT and UTC
The GMT is a watch that can display two or more time zones. Either way, the most straightforward way to display multiple time zones is to have multiple movements in a single case. However, the most economical and common method is to attach a rotating bezel with a 12-hour or 24-hour scale. The use method of rotating the bezel is very simple, the dial corresponding to the number of time zone 2 can be aligned with the dial hour, if the dial time is London time, then turn the bezel clockwise for 1 hour, indicating the Continental European time, counter-clockwise for 8 hours, is the West Coast time of the United States.
Setting the dial time to home or destination depends on the user's preference, but since a 12-hour watch cannot distinguish between day and night, it is usually reasonable to set the location time. One event has complicated the definition of GMT: on 1 January 1972, UTC (Coordinated Universal Time) became the new world standard time.
For convenience, it is usually denoted as Universal Time Coordinated. Also for convenience, GMT and UTC are generally considered the same without needing to be accurate to the second. Although the UTC is more scientific and precise, it is still more popular with watch players and collectors. UTC is seen by many as a means for Paris to plot its place as the timing centre of the world. In fact, it is a time measurement system based on atomic time and as close to universal time as possible. It came about because of the modern need for accurate timing.
Unlike previous timekeeping systems, the UTC is very accurate and does not measure the average solar time at a particular place. But the time difference between UTC and universal time accumulates when the earth's rotation is not uniform, so the UTC adds a leap second or two after one period to compensate. As a result, there are some integer second differences between COORDINATED Universal time and international Atomic Time (TAI). The Paris-based International Earth Rotation Center (IERS) is responsible for deciding when to add leap seconds.

Beijing time is 8 time zones different from GMT or UTC time, Beijing, Shanghai and Chongqing are located in the East 8 zone, so Beijing time 2013-1-0:00:00, converted to UTC time: Tue Jan 1 00:00:00 UTC+0800 2013, 8 hours have passed.
2. Local time to UTC time conversion
The steps for the conversion from local time to UTC time are as follows:

1. Converts a string date to a date data type
If you already have a date type, you can omit this step.

The transformation can be done using the functions in the following example.
2. Obtain UTC date data
Includes year, month, day, hour and second, obtained using getUTC***() method.

Acquired year: var y = ES67en. getUTCFullYear();
Acquired month: var m = date.getUTCMonth();
Acquisition date: var d = ES77en. getUTCDate();
Acquisition hour: var h= ES82en. getUTCHours();
Get minutes: var M = date. getUTCMinutes();
Get seconds: var s = date. getUTCSeconds();

Here date is date-type data.

Note: Using the method without UTC here is problematic (for example: date.getFullYear, date.getMonth) and results will be wrong when you do the next step conversion.

3. Use Date.UTC () function for conversion
Convert the date data obtained in Step 2 to the UTC time (actually the number of milliseconds since 1700)

var utc = Date.UTC(y,m,d,h,M,s);

Here, y, m, d, h, M and s represent the year, month, day, hour, minute and second values obtained in step 2, respectively.

3. UTC date to local date conversion
The UTC date to local date conversion is much simpler, converting the UTC time to the date format first and then to the local date format, for example:


var date2 = new Date(utc);

var localeString = date2.toLocaleString();

Or just the date


var localeDateString = date2.toLocaleDateString();

Or just time


var localeTimeString = date2.toLocaleTimeString();

Example:


// Date plus or minus  
function dateadd(sdate, delta, ymdh){ 
 if(!sdate ) return; 
  
 if(typeof sdate == 'object') sdate = sdate.toLocaleString(); 
 
 /(\d{4})[\D](\d{1,2})[\D](\d{1,2})[\D]?\s(\d{1,2}):(\d{1,2}):(\d{1,2})/.exec(sdate); 
 var a = [0,0,0,0]; 
  
 switch(ymdh){ 
  case 'y': 
   a = [delta, -1, 0, 0]; 
   break; 
  case 'm': 
   a=[0, delta-1, 0, 0]; 
   break; 
  case 'H': 
   a=[0, -1, 0, delta]; 
   break;  
  default: 
   a = [0, -1, delta, 0]; 
   break;   
 } 
  
 println('date:' + (parseInt(RegExp.$1)+ a[0]) + '-'+ (parseInt(RegExp.$2)+a[1]) +'-' + (parseInt(RegExp.$3)+a[2]) 
  + ' ' + (parseInt(RegExp.$4)+a[3]) +':' + RegExp.$5 + ':' +RegExp.$6); 
  
 return new Date(parseInt(RegExp.$1)+ a[0], parseInt(RegExp.$2)+a[1], parseInt(RegExp.$3)+a[2], parseInt(RegExp.$4)+a[3], RegExp.$5,RegExp.$6); 
} 
 
 //UTC conversion  
 println('---------------------------------------------'); 
 var sdate='2013-01-01 00:00:00.0'; 
 var d = dateadd(sdate,0); 
 var d2 = Date.UTC(d.getUTCFullYear(),d.getUTCMonth() ,d.getUTCDate(),d.getUTCHours(),d.getUTCMinutes(),d.getUTCSeconds()); 
 println(' The original date: ' + sdate); 
 println('d2:' + d2); 
 println('d3:' + new Date(d2)); 
 println('d4:' + new Date(d2).toLocaleString()); 
 println('d5:' + d2.toLocaleString()); 

Test results:


---------------------------------------------
date:2013-0-1 0:00:00
 The original date: 2013-01-01 00:00:00.0
d2:1356969600000
d3:Tue Jan 1 00:00:00 UTC+0800 2013
d4:2013 years 1 month 1 day  0:00:00
d5:1,356,969,600,000.00

You can see that the UTC time is actually a long string of Numbers expressed in milliseconds since 1970.

Above is the entire content of this article, I hope to help you with your study.


Related articles: