當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JS将指定的时间戳转为UTC时间
生活随笔
收集整理的這篇文章主要介紹了
JS将指定的时间戳转为UTC时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Js中獲取時間戳可用var dayMiliseconds = parseInt(new Date().valueOf());Js的時間戳單位為毫秒(1s = 1000 ms),下面是一個將制定的格式轉化成UTC時間的函數。
//format the date string from webservice to UTC time; function toUTCtime(dateStr) {//Date(1381243615503+0530),1381243615503,(1381243615503+0800) dateStr += "";var utcPrefix = 0;var offset = 0;var dateFormatString = "yyyy-MM-dd hh:mm:ss";var utcTimeString = "";var totalMiliseconds = 0;var regMatchNums = /\d+/gi;var regSign = /[\+|\-]/gi;var arrNums = dateStr.match(regMatchNums);utcPrefix = parseInt(arrNums[0]);if (arrNums.length > 1) {offset = arrNums[1];offsetHour = offset.substring(0, 2);offsetMin = offset.substring(2, 4);offset = parseInt(offsetHour) * 60 * 60 * 1000 + parseInt(offsetMin) * 60 * 1000;}if(dateStr.lastIndexOf("+")>-1){totalMiliseconds= utcPrefix - offset;} else if (dateStr.lastIndexOf("-") > -1) {totalMiliseconds = utcPrefix + offset;}utcTimeString = new Date(totalMiliseconds).format(dateFormatString);return utcTimeString;}?
轉載于:https://www.cnblogs.com/skybreak/p/3361608.html
總結
以上是生活随笔為你收集整理的JS将指定的时间戳转为UTC时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 免费的.NET混淆和反编译工具
- 下一篇: iOS 为tableview添加新的ce