Detailed description of the JavaScript UTC time conversion method, javascriptutc

Source: Internet
Author: User
Tags time zones

Detailed description of the JavaScript UTC time conversion method, javascriptutc

I. Preface
1. UTC: Universal Time Coordinated, coordinates the Universal Time.
2. Greenwich Mean Time (GMT)
Greenwich Mean Time (GMT) refers to the standard Time of the Royal Greenwich Observatory on the outskirts of London, because the meridian is defined through the meridian there. Theoretically, midday Greenwich mean the time when the Sun crosses the Greenwich Meridian. Because the earth's motion speed in its elliptical orbit is uneven, this time may be 16 minutes different from the actual sun time. The daily rotation of the earth is somewhat irregular and is slowing down slowly. Therefore, Greenwich Mean Time is no longer used as the standard time. The current standard time-Coordinated Universal Time (UTC)-is provided by an atomic clock. Since January 1, February 5, 1924, the Greenwich Mean observatory will distribute Time-of-adjustment information to the world every hour. While UTC is the accurate time provided based on the standard GMT.
GMT (Greenwich Mean Time)-Greenwich Mean Time, which is the benchmark Time of the British Empire in the middle of the 19th century and the de facto world benchmark Time. At that time, it mainly served the railway system after 1840. It divides the world into 24 time zones based on the Greenwich Observatory's zero-degree meridian, in addition to being affected by racism, nationalism and some anti-British sentiments in a specific period, its position has never been shaken.

Difference between GMT and UTC
The GMT watch is a watch that can display two or more time zones. No matter how it is used, the most direct way to display multiple time zones is to install multiple movements in one case. However, the most economical and common method is to append a circle with a 12-hour or 24-hour scale. The usage of rotating the table ring is very simple. Just align the number corresponding to the second time zone on the table ring to the hour hand of the dial. If the dial time is London time, turn the table circle clockwise for one hour, it indicates continental Europe time, turning clockwise for eight hours, then the West Coast Time of the United States.
Setting the dial time to hometown time or destination time depends on the user's preference. However, because the 12-hour watch cannot identify the day and night time, it is usually reasonable to set the location time. The occurrence of an event complicate the definition of GMT: January 1, 1972, UTC (Coordinated Universal Time) became the new World Standard Time.
For convenience, it is usually recorded as Universal Time Coordinated. For convenience, gmtr and UTC are usually treated as equivalent without accurate to seconds. Although UTC is more scientific and accurate, GMT is still more popular for watch players and collectors. Many believe that UTC is a means for Paris to plot the world's central timing position. In fact, it is a time metering system that is based on atomic time and tries to be as close as possible to the world at the moment. Its emergence is the need of modern society for accurate timing.
The atomic time is different from the previous timing system. It is very accurate and does not use the average sun time of a certain place as the benchmark. However, when the Earth's rotation speed is uneven, the time difference between the atomic time and the Universal Time will accumulate over time, because of this, UTC will add a positive or negative leap second after a period of time to compensate. Therefore, there will be several integer-second differences between the Coordinated Universal Time and the international atomic time (TAI. The Paris-based IERS is responsible for deciding when to join a leap second.

Beijing time differs from Greenwich time or UTC time by eight time zones. Beijing, Shanghai, and Chongqing are located in the east 8 zone. Therefore, the UTC time is converted to 0:00:00 Beijing time: tue Jan 1 00:00:00 UTC + 0800 2013, time passed 8 hours.
Ii. Conversion from local time to UTC time
To convert the local time to UTC time, follow these steps:

1. Convert string date to date data type
If the date type is already in use, skip this step.

You can use the functions in the following example to convert.
2. Get UTC date data
Including year, month, day, hour, minute, and second. Use the getUTC *** () method to obtain the value.

Obtain the year: var y = date. getUTCFullYear ();
Get month: var m = date. getUTCMonth ();
Get date: var d = date. getUTCDate ();
Get hour: var h = date. getUTCHours ();
Get minutes: var M = date. getUTCMinutes ();
Obtain the second: var s = date. getUTCSeconds ();

Here, date is data of the date type.

Note: There is a problem with the method without UTC (for example, date. getFullYear, date. getMonth). The result is incorrect during the next conversion.

3. Use the Date. UTC () function for conversion.
Convert the date data obtained in step 1 to UTC time (in fact, the number of milliseconds since January 1, 2nd)

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.

Iii. Conversion from UTC to local date
It is much easier to convert a UTC date to a local date. First, convert the UTC time to a date format and then convert it to a local date format. For example:

var date2 = new Date(utc);var localeString = date2.toLocaleString();

Or as long as the date

var localeDateString = date2.toLocaleDateString();

Or as long as the time

var localeTimeString = date2.toLocaleTimeString();

Instance:

// Date addition and subtraction calculation
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 ('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
Original date: 2013-01-01 00: 00: 00.0
d2: 1356969600000
d3: Tue Jan 1 00:00:00 UTC + 0800 2013
d4: January 1, 2013 0:00:00
d5: 1,356,969,600,000.00 

We can see that the UTC time is actually a string of long numbers expressed in milliseconds since January 1, 1970.

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • The JavaScript script converts the local time to another time zone.
  • Extjs shows the problem after the time taken from the database is converted to JSON
  • How does JS convert the UTC format time to the local format?
  • Sample Code for converting time from millisecond to date in js
  • How JavaScript converts the current time to the UTC Standard Time
  • JavaScript time conversion Handler
  • Use javascript to convert the time format to today, yesterday, and the day before yesterday.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.