简体   繁体   English

将 unix 时间戳转换为特定时区

[英]Convert unix timestamp to specific timezone

I have an timestamp in unix like below which is in +2:00 timezone, but I want to get this same date but in +0:00 timezone, with using dayjs ,我在 unix 中有一个时间戳,如下所示,位于+2:00时区,但我想使用dayjs获得相同的日期,但在+0:00时区,

console.log("TIMESTAMP: ", dayjs.unix(timestamp).format());
>  TIMESTAMP:  2021-05-20T13:46:07+02:00

console.log("FIXED TIMESTAMP: ", dayjs.unix(timestamp).tz("Europe/London").format());
>  FIXED TIMESTAMP:  2021-05-20T12:46:07+01:00

above I try to do this with tz("Europe/London") but, I don't know why, I've got date in "+01:00" timezone..., Why this function do not return me new timestamp converted to "+0:00"?上面我尝试使用tz("Europe/London")执行此操作,但是,我不知道为什么,我的日期在“+01:00”时区...,为什么这个函数不返回我新的时间戳转换为“+0:00”?

Thanks for any help!谢谢你的帮助!

You have to include a couple of extender libraries for both UTC and Timezone formatting to work in dayjs.您必须包含几个扩展库,以便 UTC 和时区格式在 dayjs 中工作。 Both the UTC and Timezone Javascript files are required . UTC 和时区 Javascript 文件都是必需的。 I used version 1.10.7 because those were the most up to date.我使用了 1.10.7 版本,因为它们是最新的。

If you're working in a browser include the following source.如果您在浏览器中工作,请包括以下来源。 HTML: HTML:

<script src="https://unpkg.com/dayjs@1.10.7/dayjs.min.js"></script>
<script src="https://unpkg.com/dayjs@1.10.7/plugin/utc.js"></script>
<script src="https://unpkg.com/dayjs@1.10.7/plugin/timezone.js"></script>

JavaScript: JavaScript:

dayjs.extend(window.dayjs_plugin_utc);
dayjs.extend(window.dayjs_plugin_timezone);

let printFormat = 'hh:mm:ssA';
let nowLocal = dayjs().utc().local().format(printFormat);
console.log(nowLocal);
console.log(dayjs().tz("America/New_York").format(printFormat));
console.log(dayjs().tz("Asia/Tokyo").format(printFormat));

A couple of notes for your attempt, the 'tz' (Timezone) object does not hang off the unix() function, it is attached directly to the top level dayjs object.对于您的尝试有几点注意事项,“tz”(时区)对象不会挂起 unix() 函数,它直接附加到顶级 dayjs 对象。 The above example takes the local time, and prints it in three different time zones each in the format hh:mm:ssAM|PM .上面的示例采用本地时间,并以 hh:mm:ssAM|PM 格式在三个不同的时区打印。 The unix function takes an Unix timestamp and returns a DayJS object. unix 函数接受一个 Unix 时间戳并返回一个 DayJS 对象。

Here's a JSFiddle with the above example running: https://jsfiddle.net/e4x507p3/2/这是运行上述示例的 JSFiddle: https ://jsfiddle.net/e4x507p3/2/

Note, if you're using extensions in a browser, this should work, there is a different method required if you're working in NodeJS: https://day.js.org/docs/en/plugin/loading-into-nodejs请注意,如果您在浏览器中使用扩展,这应该可以工作,如果您在 NodeJS 中工作,则需要不同的方法: https ://day.js.org/docs/en/plugin/loading-into-节点

Have you tried this plug in?你试过这个插件吗?

dayjs().utc().format()

Here is a little sandbox.是一个小沙盒。

Although it wont show you +0:00 timestamp, because it's presented in different ISO standard.虽然它不会显示 +0:00 时间戳,因为它以不同的 ISO 标准呈现。 Will that work for you?这对你有用吗?

import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
import utc from 'dayjs/plugin/utc.js'
import timezone from 'dayjs/plugin/timezone.js'
dayjs.extend(customParseFormat)
dayjs.extend(utc)
dayjs.extend(timezone)

const datetime = dayjs.unix(1653134400)

console.log(datetime.tz('UTC').format('YYYY-MM-DD HH:mm:ss Z')) // 2022-05-21 12:00:00 +00:00
console.log(
    datetime.tz('America/Los_Angeles').format('YYYY-MM-DD HH:mm:ss Z') // 2022-05-21 05:00:00 -07:00
)
console.log(datetime.tz('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss Z')) // 2022-05-21 20:00:00 +08:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM