Detailed JavaScript UTC time conversion method _javascript Tips

Source: Internet
Author: User
Tags time zones local time

First, preface
1, utc:universal time coordinated, coordinate the world.
2, GMT (GMT)
    Greenwich Mean Time (Greenwich Mean time,gmt) is the standard time for the Royal Greenwich Observatory, located in the suburbs of London, Because the meridian is defined by the warp through there. In theory, the midday of Greenwich Mean time is the time when the Sun crosses the Greenwich Meridian. Because the Earth's motion in its elliptical orbit is uneven, this moment may be 16 minutes apart from the actual sun. The daily rotation of the earth is somewhat irregular, and it is slowing down slowly. So, GMT is no longer used as a standard time. The standard time now-Coordinated Universal Time (UTC)-is provided by an atomic clock. Since February 5, 1924, the Greenwich Observatory has been disseminating information to the world every hour. UTC is the exact time provided based on the standard GMT.
    GMT (Greenwich Mean time)-GMT, Greenwich Standard Time is the base time for the Daying Empire in the 19th century century, as well as the de facto world base time. At that time, mainly for the 1840 years after the railway system services. It divides the world into 24 time zones with a longitude of 0 degrees at Greenwich Observatory, which has never wavered except for a particular period of xenophobia, nationalism and certain anti-British moods.

the difference between GMT and UTC
    GMT watches are watches that can display two or more time zone times. In any way, the most direct way to display multiple time zones is to install multiple movements in one case. But the most economical and most common method is to attach a rotating bezel with a scale of 12 hours or 24 hours. The use of rotating bezel is simple, turn the number of the second time zone time on the bezel to the hour of the align disk, and if the dial time is London time, rotate the bezel clockwise for one hour, indicating the continental time, and turning counterclockwise for eight hours, it is the west Coast time of the United States.
    Setting the dial time as home time or destination time depends on the user's preference, but it is usually more reasonable to set the location time because the 12-hour watch cannot distinguish between day and night. The occurrence of an event complicates the definition of GMT: January 1, 1972, UTC (coordinated World time) becomes the New World standard Time.
    is usually recorded as universal Time coordinated for convenience. Also, for convenience, GMT and UTC are usually equated as if they do not need to be accurate to seconds. Although UTC is more scientific and accurate, GMT is still more popular for watch players and collectors. Many people believe that UTC is a means by which Paris plots the world's timing center. In fact, it is a time measurement system based on atomic time and as close to the world as possible at all times. Its appearance is the need of modern society for accurate timing. The
    atom differs from the previous timing system in that it is very precise and not based on the average solar time in a place, but when the earth rotates at an uneven rate, the time difference between the atom and the world is cumulative, so the UTC is compensated by a positive or negative leap second over a period of time. As a result, there are several integer seconds differences between the coordinated world and the International Atomic Time (TAI). The International Earth Rotation NCBs (IERS) in Paris is responsible for deciding when to add leap seconds.

The time between Beijing and GMT or UTC 8 time zones, Beijing, Shanghai, Chongqing is located in the East 8 district, so Beijing time 2013-1-1 0:00:00, converted to UTC time: Tue 1 00:00:00 utc+0800 2013, time after 8 hours.
second, local time to UTC time Conversion
local time to UTC time conversion, as follows:

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

You can use the functions in the following example to convert.
2. Get UTC Date Data
including month, day, time, using the getutc*** () method to obtain.

Get year: var y = date.getutcfullyear ();
Get month: var m = Date.getutcmonth ();
Get day: var d = date.getutcdate ();
Get hours: Var h= date.getutchours ();
Get minutes: var M = Date.getutcminutes ();
Get second: var s = date.getutcseconds ();

Here date is the data type.

Note: There is a problem with a method that does not have UTC (for example: Date.getfullyear,date.getmonth), and a result error occurs when the next conversion is made.

3, using the DATE.UTC () function for conversion
Converts the date data obtained in step 2nd to 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, s represent the year, month, day, time, minute, and second values obtained in step 2.

Conversion of UTC date to local date
the UTC date to the local date conversion is much simpler, first converting the UTC time to date format, and then to the 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 time

var localetimestring = date2.tolocaletimestring ();

Instance:

Date subtraction calculates 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 convert 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 1 00:00:00 utc+0800 2013 d4:2013
year January 1 0:00:00 d5:1,356,969,600,000.00


You can see that UTC time is actually a string of long numbers represented by the number of milliseconds since 1970.

The above is the entire content of this article, I hope to help you learn.

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.