最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert unix timestamp to specific timezone - Stack Overflow

programmeradmin1浏览0评论

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,

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"?

Thanks for any help!

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,

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"?

Thanks for any help!

Share Improve this question asked Nov 4, 2021 at 17:40 markWankamarkWanka 7943 gold badges21 silver badges38 bronze badges 2
  • "+2:00" is an offset. The concept of "timezone" typically includes historic changes to offset and daylight saving for a place (e.g. "Europe/London") or geographic region (e.g. "Central European Time"). Many offsets map to multiple timezones. While "Europe/London" has offset +0 for some of the year, it also has used DST (+1) for the rest of the year on and off over the last century and for a short time had "double DST" (i.e. +2) all year round. – RobG Commented Nov 4, 2021 at 20:43
  • Oh, on 20 May 2021 London was on daylight saving, hence +1. Using locations to get a particular offset is fraught as places change their offset from time to time, not just because of DST. If you want +0, then use "UTC" as the location/timezone. – RobG Commented Nov 5, 2021 at 9:01
Add a ment  | 

3 Answers 3

Reset to default 2
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

You have to include a couple of extender libraries for both UTC and Timezone formatting to work in dayjs. Both the UTC and Timezone Javascript files are required . I used version 1.10.7 because those were the most up to date.

If you're working in a browser include the following source. HTML:

<script src="https://unpkg./[email protected]/dayjs.min.js"></script>
<script src="https://unpkg./[email protected]/plugin/utc.js"></script>
<script src="https://unpkg./[email protected]/plugin/timezone.js"></script>

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. The above example takes the local time, and prints it in three different time zones each in the format hh:mm:ssAM|PM . The unix function takes an Unix timestamp and returns a DayJS object.

Here's a JSFiddle with the above example running: https://jsfiddle/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/docs/en/plugin/loading-into-nodejs

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. Will that work for you?

发布评论

评论列表(0)

  1. 暂无评论