简体   繁体   中英

JS How to change timezone without changing locale (time format)

I get time from backend as number. I set the date by:

const time = 1571307720000;
const date = new Date();
date.setTime(time);
date.toLocaleString();

I will get timezone from backend and I want to set it in toLocaleString() but I don't want to change locale(time format) but in every page I see:

date.toLocaleString('es-US', { timeZone: 'Asia/Seoul' })

and I can't use date.toLocaleString function without first string.

Is it possible to set timezone but use locale time format?

According to the MDN entry for Date.prototype.toLocaleString() ,

See the Intl.DateTimeFormat() constructor for details on these parameters and how to use them.

And the entry for Intl.DateTimeFormat() says,

To use the browser's default locale, pass an empty array.

So:

date.toLocaleString([], { timeZone: 'Asia/Seoul' })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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