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()
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
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-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17682121.html
未经授权禁止转载,违者必究!

浙公网安备 33010602011771号