JavaScript Date UTC & GMT All In One
JavaScript Date UTC & GMT All In One
js 时期
时区转换
UTC
& GMT
Coordinated Universal Time / 协调世界时
UTC 是最主要的世界时间标准
,其以原子
时的秒长为基础,在时刻上尽量接近
于格林威治标准时间。
UTC 实际上与 GMT 基本相同.
如果本地时间比UTC时间快
,例如中国大陆、香港、澳门、台湾、蒙古国、菲律宾、新加坡、马来西亚、澳大利亚西部的时间比UTC快8小时,就会写作UTC+8
,俗称东八区
。
相反,如果本地时间比UTC时间慢
,例如夏威夷的时间比UTC时间慢10小时,就会写作UTC-10
,俗称西十区
。
https://zh.wikipedia.org/zh-hans/协调世界时
Greenwich Mean Time / 格林威治标准时间 (英国)
https://zh.wikipedia.org/zh-hans/格林尼治標準時間
使用场景
GitHub Actions 定时任务时间戳不准确 bug
const date = new Date();
const month = `${date.getMonth()+ 1}`.padStart(2, `0`)
const day = `${date.getDate()}`.padStart(2, `0`)
const hour = `${date.getHours()}`.padStart(2, `0`)
const minute = `${date.getMinutes()}`.padStart(2, `0`)
this.date = date.toString();
// this.date = date.toUTCString();
// this.date = date.toGMTString();
// this.date = date.toISOString();
convert UTC+0
to UTC+8
/GMT+0800
timezone
+8
tips: 左加右减 / 西减东加
new Date()
// Wed Sep 06 2023 03:14:43 GMT+0800 (China Standard Time)
new Date(`${new Date().toUTCString()}+0800`);
// Tue Sep 05 2023 19:14:47 GMT+0800 (China Standard Time)
new Date(`${new Date().toUTCString()}-0800`);
// Wed Sep 06 2023 11:15:16 GMT+0800 (China Standard Time)
const changeUTCTimeToChinaTime = () => {
const timestamp = Date.now();
// GMT+0800 (China Standard Time)
// 28800000 毫秒 = 60 分/时 * 8 时 * 60 秒/分 * 1000 毫秒/秒
return new Date(timestamp + 60 * 8 * 60 * 1000)
}
demo
const date = new Date();
// Thu Sep 07 2023 02:12:26 GMT+0800 (China Standard Time)
const changeUTCTimeToChinaTime = () => {
const timestamp = Date.now();
// GMT+0800 (China Standard Time)
// 28800000 毫秒 = 60 分/时 * 8 时 * 60 秒/分 * 1000 毫秒/秒
return new Date(timestamp + 60 * 8 * 60 * 1000)
}
changeUTCTimeToChinaTime()
// Thu Sep 07 2023 10:12:33 GMT+0800 (China Standard Time)
https://github.com/web-full-stack/cyclic-express-server/blob/main/crawler.js
// UTC to China + 8
const convertUTCTimeToChinaTime = () => {
const date = new Date()
const cmtTime = date.toGMTString()
// GMT+0800 (China Standard Time)
// 480 分 = 60 分/时 * 8 时
// return new Date(utcTime +480);
const str = new Date(`${cmtTime}+0800`).toGMTString();
const time = str.split(` `)[4];
// return new Date(`${cmtTime}+0800`);
// return new Date(`${date.toGMTString()}+0800`).toGMTString();
}
const china = convertUTCTimeToChinaTime();
demos
function changeTimezone(us) {
// suppose the date is 00:00 UTC
const china = new Date(us.toLocaleString('zh-CN', {timeZone: "Asia/Shanghai"}));
// const china = new Date(us.toLocaleString('en-US', { timeZone: 'America/New_York'}));
// it will be 08:00 in China and the diff is 8 hours
const diff = us.getTime() - china.getTime();
return new Date(us.getTime() - diff);
}
function getChinaTime() {
const us = new Date();
const china = changeTimezone(us);
console.log(`us: ${us.toString()}`);
console.log(`china: ${china.toString()}`);
}
// getChinaTime()
timezones 时区
UTC+0
UTC+8
refs
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
https://day.js.org/docs/en/plugin/timezone
https://www.cnblogs.com/xgqfrms/p/14097254.html
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17682121.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-09-06 cnblogs & vscode extensions All In One
2022-09-06 WebAssembly Development Guide All In One
2021-09-06 GitHub Universe: the global developer event
2020-09-06 编程视频教程制作指南 All In One
2020-09-06 ISBN
2020-09-06 ituring 挂了
2020-09-06 PM2 & nodemon & Node.js Deamon All In One