Javascript 如何将 IST 时区对象添加到 Moment.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32355297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:57:04  来源:igfitidea点击:

How to add IST timezone object to Moment.js

javascriptdatetimezonemomentjs

提问by Snehal Masne

I am referring to http://momentjs.com/timezone/docs/#/using-timezones/

我指的是http://momentjs.com/timezone/docs/#/using-timezones/

In example, its given for below locations :

例如,它针对以下位置给出:

moment.tz.add([
    'America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0',
    'America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0'
]);

I am not sure of IST values as its given for :

我不确定它给出的 IST 值:

{
    name    : 'America/Los_Angeles',          // the unique identifier
    abbrs   : ['PDT', 'PST'],                 // the abbreviations
    untils  : [1414918800000, 1425808800000], // the timestamps in milliseconds
    offsets : [420, 480]                      // the offsets in minutes
}

Could someone help in adding IST object to this?

有人可以帮助添加 IST 对象吗?

回答by Matt Johnson-Pint

The easiest way is to just use one of the files that includes time zone data from the moment-timezone home page, such as moment-timezone-2010-2020.jsor moment-timezone-all-years.js(or the minified versions).

最简单的方法是只使用包含来自moment-timezone 主页的时区数据的文件之一,例如moment-timezone-2010-2020.jsor moment-timezone-all-years.js(或缩小版本)。

You can however add just a single zone if you prefer. You can find the zone's packed string within the data files listed above. For India, the full zone definition is:

但是,如果您愿意,您可以只添加一个区域。您可以在上面列出的数据文件中找到区域的压缩字符串。对于印度,完整的区域定义是:

moment.tz.add("Asia/Calcutta|HMT BURT IST IST|-5R.k -6u -5u -6u|01232|-18LFR.k 1unn.k HB0 7zX0");
moment.tz.link("Asia/Calcutta|Asia/Kolkata");

You may or may not need the link, depending on what exactly you're doing.

您可能需要也可能不需要链接,具体取决于您在做什么。

Note that I assumed that by "IST" you mean India Standard Time, and not Irish Standard Time or Israel Standard Time. Those zones would have different data, of course.

请注意,我假设“IST”是指印度标准时间,而不是爱尔兰标准时间或以色列标准时间。当然,这些区域会有不同的数据。

Also, since India has been on UTC+5:30 since 1942, you may just wish to use a fixed offset - which would eliminate the need to use moment-timezone. Just use the utcOffsetfunction in moment. For example, both of the following will return the current time in India.

此外,由于印度自 1942 年以来一直使用 UTC+5:30,您可能只想使用固定偏移量 - 这将消除使用时刻时区的需要。只需立即使用该utcOffset功能。例如,以下两个都将返回印度的当前时间。

moment().utcOffset("+05:30").format()

or

或者

moment().utcOffset(330).format()