I have a moment object like below
我有一个像下面的时刻对象
from which i want to extract the time zone value "PDT". How can i get this value using moment?
从中我想提取时区值“PDT”。我怎样才能获得这个价值?
i have tried
我试过了
moment.tz(this.startDate.toString()).zoneAbbr();
but that returns a value "UTC"
但是返回值“UTC”
Any suggestions?
3 个解决方案
#1
0
I am Afraid you will get this only.. Since this is how the getZoneAbbr function works.
我很害怕你只会得到这个..因为这就是getZoneAbbr函数的工作方式。
function getZoneAbbr () {
return this._isUTC ? 'UTC' : '';
}
Now, This is a problem. UTC is what one can use with timeOffest to determine which TimeZone you are lying in. and only way to do it to make a list of Time zone names with their offset values, then compare which offset value you get. then you can pick your time zone.
现在,这是一个问题。 UTC是可以与timeOffest一起使用来确定您所在的TimeZone的唯一方法。只有这样做才能使用其偏移值创建时区名称列表,然后比较您获得的偏移值。然后你可以选择你的时区。
Even the normal JavaScript Date function do not have this support as in yet. you will have to create that offset and time zone name map yourself.
即使是普通的JavaScript Date函数也没有这种支持。您必须自己创建该偏移和时区名称映射。
#2
0
You might wan't to add the specific timezone you're in for the date that you parse like following:
您可能不想为您解析的日期添加您所在的特定时区,如下所示:
moment.tz(moment.now(), 'America/New_York').zoneAbbr();
#3
0
Moment had the z
/ zz
format parameter to get abbreviated time zone name, but as the doc says:
Moment有z / zz格式参数来获得缩写的时区名称,但正如doc所说:
Note: as of 1.6.0, the z/zz format tokens have been deprecated from plain moment objects. Read more about it here. However, they do work if you are using a specific time zone with the moment-timezone addon.
注意:从1.6.0开始,z / zz格式标记已从普通时刻对象中弃用。在这里阅读更多相关信息。但是,如果您使用具有时刻 - 时区插件的特定时区,它们确实有效。
In the linked github issue, moment developer suggest to use moment.tz.guess()
when creating moment object to have good chances of getting enviroment's timezone.
在链接的github问题中,时刻开发人员建议在创建时刻对象时使用moment.tz.guess()以获得获得环境时区的机会。
Here a working code example that you can use to get abbreviated time zone name:
这是一个可用于获取缩写时区名称的工作代码示例:
var startDate = new Date();
var momObj = moment.tz(startDate, moment.tz.guess());
var zoneAbbr1 = momObj.zoneAbbr();
console.log(zoneAbbr1);
var zoneAbbr2 = momObj.format('z')
console.log(zoneAbbr2);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.5/moment-timezone-with-data-2010-2020.min.js"></script>
#1
0
I am Afraid you will get this only.. Since this is how the getZoneAbbr function works.
我很害怕你只会得到这个..因为这就是getZoneAbbr函数的工作方式。
function getZoneAbbr () {
return this._isUTC ? 'UTC' : '';
}
Now, This is a problem. UTC is what one can use with timeOffest to determine which TimeZone you are lying in. and only way to do it to make a list of Time zone names with their offset values, then compare which offset value you get. then you can pick your time zone.
现在,这是一个问题。 UTC是可以与timeOffest一起使用来确定您所在的TimeZone的唯一方法。只有这样做才能使用其偏移值创建时区名称列表,然后比较您获得的偏移值。然后你可以选择你的时区。
Even the normal JavaScript Date function do not have this support as in yet. you will have to create that offset and time zone name map yourself.
即使是普通的JavaScript Date函数也没有这种支持。您必须自己创建该偏移和时区名称映射。
#2
0
You might wan't to add the specific timezone you're in for the date that you parse like following:
您可能不想为您解析的日期添加您所在的特定时区,如下所示:
moment.tz(moment.now(), 'America/New_York').zoneAbbr();
#3
0
Moment had the z
/ zz
format parameter to get abbreviated time zone name, but as the doc says:
Moment有z / zz格式参数来获得缩写的时区名称,但正如doc所说:
Note: as of 1.6.0, the z/zz format tokens have been deprecated from plain moment objects. Read more about it here. However, they do work if you are using a specific time zone with the moment-timezone addon.
注意:从1.6.0开始,z / zz格式标记已从普通时刻对象中弃用。在这里阅读更多相关信息。但是,如果您使用具有时刻 - 时区插件的特定时区,它们确实有效。
In the linked github issue, moment developer suggest to use moment.tz.guess()
when creating moment object to have good chances of getting enviroment's timezone.
在链接的github问题中,时刻开发人员建议在创建时刻对象时使用moment.tz.guess()以获得获得环境时区的机会。
Here a working code example that you can use to get abbreviated time zone name:
这是一个可用于获取缩写时区名称的工作代码示例:
var startDate = new Date();
var momObj = moment.tz(startDate, moment.tz.guess());
var zoneAbbr1 = momObj.zoneAbbr();
console.log(zoneAbbr1);
var zoneAbbr2 = momObj.format('z')
console.log(zoneAbbr2);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.5/moment-timezone-with-data-2010-2020.min.js"></script>