简体   繁体   English

检索已经输入的时区偏移量

[英]Retrieving the timezone offset that was already input

I create a date like this: 我创建一个像这样的日期:

var date = new Date('Wed, 19 Mar 2014 18:17:00 +0200');

This resolves to: 解决为:

Wed Mar 19 2014 17:17:00 GMT+0100 (Central European Standard Time)

Is there a way to retrieve the "+0200" portion from the date object once it is created? 创建日期对象后,是否可以从日期对象中检索“ +0200”部分? I am trying to get this without parsing the input string and without the use of external libraries. 我试图在不解析输入字符串且不使用外部库的情况下获得此信息。

EDIT: When I use 编辑:当我使用

date.getTimezoneOffset();

It returns "-60", which corresponds to the local timezone offset, which in my case is GMT+0100. 它返回“ -60”,它对应于本地时区偏移量,在我的情况下为GMT + 0100。 The question I am asking is whether the "+0200" from the input is lost in the Date object upon creation, or is it stored somewhere? 我要问的问题是,输入中的“ +0200”是在创建时丢失在Date对象中,还是存储在某处?

You can retrieve the timezoneoffset with date.getTimezoneOffset(); 您可以使用date.getTimezoneOffset();检索timezoneoffset date.getTimezoneOffset(); and use that to calculate it 并用它来计算

From the Mozilla MSDN Mozilla MSDN

The getTimezoneOffset() method returns the time-zone offset from UTC, in minutes, for the current locale. getTimezoneOffset()方法以分钟为单位返回当前语言环境与UTC的时区偏移量。

date.getTimezoneOffset()

The time-zone offset is the difference, in minutes, between UTC and local time. 时区偏移量是UTC与本地时间之间的差(以分钟为单位)。 Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. 请注意,这意味着如果本地时区晚于UTC,则偏移量为正,如果提前,则偏移量为负。 For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. 例如,如果您的时区是UTC + 10(澳大利亚东部标准时间),则将返回-600。 Daylight saving time prevents this value from being a constant even for a given locale. 夏时制即使在给定的语言环境下也可以防止该值保持恒定。


A date object is stored as the number of milliseconds since the Unix epoch. 日期对象存储为自Unix时代以来的毫秒数。 So your input string is parsed and stored as a primitive number. 因此,您的输入字符串将被解析并存储为原始数字。 So no, you can't retrieve aspects of your original input after it has been converted to a date object. 因此,不可以,在原始输入转换为日期对象后,您将无法检索原始输入。

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

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