Javascript 从时区标识符获取当前GMT偏移量
如何从时区标识符获取当前GMT偏移量?理想情况下,它也会包含长格式名称 例如:Javascript 从时区标识符获取当前GMT偏移量,javascript,datetime,timezone,momentjs,moment-timezone,Javascript,Datetime,Timezone,Momentjs,Moment Timezone,如何从时区标识符获取当前GMT偏移量?理想情况下,它也会包含长格式名称 例如: "America/Los_Angeles" //output: GMT-0700 (Pacific Daylight Time) 2020-12-21T03:57:00Z //output: GMT-0800 (Pacific Standard Time) 如果它也能处理ISO字符串,那就太好了,例如: "America/Los_Angeles" //output: GMT-0700 (Pacific D
"America/Los_Angeles" //output: GMT-0700 (Pacific Daylight Time)
2020-12-21T03:57:00Z //output: GMT-0800 (Pacific Standard Time)
如果它也能处理ISO字符串,那就太好了,例如:
"America/Los_Angeles" //output: GMT-0700 (Pacific Daylight Time)
2020-12-21T03:57:00Z //output: GMT-0800 (Pacific Standard Time)
您可以使用以下对象的时区和时区名称选项来获取更常见时区的名称,但不太为人所知的时区可能会丢失。此外:
//获取短偏移量,可能显示实际偏移量,但可能是短名称
设formatterA=new Intl.DateTimeFormat('en',{时区:'America/new_York',时区名称:'short'});
console.log(formatterA.format(new Date());//美国东部时间2020年5月2日
//获取短偏移量,可能显示实际偏移量,但可能是短名称
设formatterB=new Intl.DateTimeFormat('en',{时区:'America/new_York',时区名称:'long'});
console.log(formatterB.format(new Date());//2020年5月2日,东部夏时制
您可以使用以下对象的时区和时区名称选项来获取更常见时区的名称,但不太知名的时区可能会丢失。此外:
//获取短偏移量,可能显示实际偏移量,但可能是短名称
设formatterA=new Intl.DateTimeFormat('en',{时区:'America/new_York',时区名称:'short'});
console.log(formatterA.format(new Date());//美国东部时间2020年5月2日
//获取短偏移量,可能显示实际偏移量,但可能是短名称
设formatterB=new Intl.DateTimeFormat('en',{时区:'America/new_York',时区名称:'long'});
console.log(formatterB.format(new Date());//2020年5月2日,东部夏时制
@yogadog内置Date.prototype.toString生成的字符串在中定义为包含偏移值,但该字符串取决于实现且可选,且名称和偏移量仅在主机时区中,因此实际上与问题无关。@yogadog内置Date.prototype.toString生成的字符串在中定义为包含偏移量值,但是该字符串是依赖于实现的可选字符串,并且名称和偏移量仅在主机时区中,因此与问题无关。