首頁 > web前端 > js教程 > 主體

詳解JavaScript UTC時間轉換方法_javascript技巧

WBOY
發布: 2016-05-16 15:21:21
原創
2537 人瀏覽過

1. Foreword
1. UTC: Universal Time Coordinated, coordinated universal time.
2. Greenwich Mean Time (GMT)
Greenwich Mean Time (GMT) refers to the standard time at the Royal Greenwich Observatory in the suburbs of London, because the prime meridian is defined as the longitude passing there. Theoretically, noon in Greenwich Mean Time is when the sun crosses the Greenwich meridian. Due to the uneven motion of the Earth in its elliptical orbit, this time may differ from actual solar time by 16 minutes. The Earth's daily rotation is somewhat irregular and is slowly slowing down. Therefore, GMT is no longer used as standard time. Today's standard time - Coordinated Universal Time (UTC) - is provided by atomic clocks. Since February 5, 1924, the Greenwich Observatory has sent time adjustment information to the world every hour. UTC is based on the accurate time provided by the standard GMT.
GMT (Greenwich Mean Time) - Greenwich Mean Time. Greenwich Mean Time was the reference time of the British Empire in the mid-19th century and was also the de facto world reference time. At that time it was mainly used to serve the railway system after 1840. It uses the longitude of the Greenwich Observatory as the 0-degree longitude and divides the world into 24 time zones. Except for being affected by xenophobia, nationalism and some anti-British sentiments at certain times, its status has never been shaken.

The difference between GMT and UTC
A GMT watch is a watch that can display the time in two or more time zones. No matter what method is used, the most direct way to display multiple time zones is to install multiple movements in one watch case. However, the most economical and common method is to attach a rotating bezel with a 12-hour or 24-hour time scale. The method of using the rotating bezel is very simple. Just align the number on the bezel corresponding to the second time zone time with the hour hand of the dial. If the dial time is London time, then turn the bezel clockwise for one hour to indicate continental European time. , turn counterclockwise eight hours, which is the West Coast Time of the United States.
​Whether the dial time is set to home time or destination time depends on the user's preference, but since the 12-hour watch cannot distinguish between day and night, it is usually more reasonable to set the local time. An event occurred that complicated the definition of GMT: on January 1, 1972, UTC (Coordinated Universal Time) became the new universal standard time.
For convenience, it is usually recorded as Universal Time Coordinated. Also for convenience, GMT and UTC are often treated as equivalent when precision to the second is not required. Although UTC is more scientific and accurate, GMT is still more popular among watch players and collectors. Many people believe that UTC is a means for Paris to seek status as the world's timing center. In fact, it is a time measurement system based on atomic time and as close to universal time as possible. Its emergence is the need for precise timing in modern society.
Atomic time is different from previous timing systems. It is very accurate and is not based on the mean solar time of a certain place. However, when the earth's rotation speed is uneven, the time difference between atomic time and universal time accumulates over time. Therefore, UTC will be in Positive or negative leap seconds are added after a period of time to compensate. Therefore there will be a number of whole seconds difference between Coordinated Universal Time and International Atomic Time (TAI). The International Central Bureau for Earth Rotation Affairs (IERS) in Paris is responsible for deciding when to include leap seconds.

Beijing time is 8 time zones different from Greenwich Time or UTC time. Beijing, Shanghai, and Chongqing are located in the 8th East Zone, so Beijing time 2013-1-1 0:00:00, converted to UTC time is: Tue Jan 1 00 :00:00 UTC+0800 2013, 8 hours later.
2. Conversion from local time to UTC time
Conversion from local time to UTC time, the steps are as follows:

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

Can be converted using the function in the example below.
2. Get UTC date data
Including year, month, day, hour, minute and second, use getUTC***() method to obtain.

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

The date here is date type data.

Note: There is a problem with using methods without UTC here (for example: date.getFullYear, date.getMonth). When performing the next conversion, a result error will occur.

3. Use the Date.UTC() function to convert
Convert the date data obtained in step 2 to UTC time (actually milliseconds since 1700)

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

这里,y、m、d、h、M、s分别代表步骤2中获取的年、月、日、时、分、秒数值。

三、UTC日期到本地日期的转换
UTC日期到本地日期转换则要简单得多,先将UTC时间转换为日期格式,然后再转换为本地日期格式,例如:

var date2 = new Date(utc);

var localeString = date2.toLocaleString();

登入後複製

或只要日期

var localeDateString = date2.toLocaleDateString();

登入後複製

或只要时间

var localeTimeString = date2.toLocaleTimeString();

登入後複製

实例:

//日期加减计算 
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转换 
 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('原日期:' + sdate); 
 println('d2:' + d2); 
 println('d3:' + new Date(d2)); 
 println('d4:' + new Date(d2).toLocaleString()); 
 println('d5:' + d2.toLocaleString()); 
登入後複製

测试结果:

---------------------------------------------
date:2013-0-1 0:00:00
原日期:2013-01-01 00:00:00.0
d2:1356969600000
d3:Tue Jan 1 00:00:00 UTC+0800 2013
d4:2013年1月1日 0:00:00
d5:1,356,969,600,000.00

登入後複製

可以看到UTC时间实际上是一串以自1970年以来的毫秒数表示的长数字。

以上就是本文的全部内容,希望对大家的学习有所帮助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!