詳解JavaScript UTC時間轉換方法_javascript技巧
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年以来的毫秒数表示的长数字。
以上就是本文的全部内容,希望对大家的学习有所帮助。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

如何使用WebSocket和JavaScript實現線上語音辨識系統引言:隨著科技的不斷發展,語音辨識技術已成為了人工智慧領域的重要組成部分。而基於WebSocket和JavaScript實現的線上語音辨識系統,具備了低延遲、即時性和跨平台的特點,成為了廣泛應用的解決方案。本文將介紹如何使用WebSocket和JavaScript來實現線上語音辨識系

WebSocket與JavaScript:實現即時監控系統的關鍵技術引言:隨著互聯網技術的快速發展,即時監控系統在各個領域中得到了廣泛的應用。而實現即時監控的關鍵技術之一就是WebSocket與JavaScript的結合使用。本文將介紹WebSocket與JavaScript在即時監控系統中的應用,並給出程式碼範例,詳細解釋其實作原理。一、WebSocket技

如何利用JavaScript和WebSocket實現即時線上點餐系統介紹:隨著網路的普及和技術的進步,越來越多的餐廳開始提供線上點餐服務。為了實現即時線上點餐系統,我們可以利用JavaScript和WebSocket技術。 WebSocket是一種基於TCP協定的全雙工通訊協議,可實現客戶端與伺服器的即時雙向通訊。在即時線上點餐系統中,當使用者選擇菜餚並下訂單

如何使用WebSocket和JavaScript實現線上預約系統在當今數位化的時代,越來越多的業務和服務都需要提供線上預約功能。而實現一個高效、即時的線上預約系統是至關重要的。本文將介紹如何使用WebSocket和JavaScript來實作一個線上預約系統,並提供具體的程式碼範例。一、什麼是WebSocketWebSocket是一種在單一TCP連線上進行全雙工

JavaScript和WebSocket:打造高效的即時天氣預報系統引言:如今,天氣預報的準確性對於日常生活以及決策制定具有重要意義。隨著技術的發展,我們可以透過即時獲取天氣數據來提供更準確可靠的天氣預報。在本文中,我們將學習如何使用JavaScript和WebSocket技術,來建立一個高效的即時天氣預報系統。本文將透過具體的程式碼範例來展示實現的過程。 We

JavaScript教學:如何取得HTTP狀態碼,需要具體程式碼範例前言:在Web開發中,經常會涉及到與伺服器進行資料互動的場景。在與伺服器進行通訊時,我們經常需要取得傳回的HTTP狀態碼來判斷操作是否成功,並根據不同的狀態碼來進行對應的處理。本篇文章將教你如何使用JavaScript來取得HTTP狀態碼,並提供一些實用的程式碼範例。使用XMLHttpRequest

用法:在JavaScript中,insertBefore()方法用於在DOM樹中插入一個新的節點。這個方法需要兩個參數:要插入的新節點和參考節點(即新節點將要插入的位置的節點)。

JavaScript是一種廣泛應用於Web開發的程式語言,而WebSocket則是一種用於即時通訊的網路協定。結合二者的強大功能,我們可以打造一個高效率的即時影像處理系統。本文將介紹如何利用JavaScript和WebSocket來實作這個系統,並提供具體的程式碼範例。首先,我們需要明確指出即時影像處理系統的需求和目標。假設我們有一個攝影機設備,可以擷取即時的影像數
