Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add IST timezone object to Moment.js

I am referring to 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 :

{
    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?

like image 690
Snehal Masne Avatar asked Sep 02 '15 14:09

Snehal Masne


1 Answers

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.js or moment-timezone-all-years.js (or the minified versions).

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.

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 utcOffset function in moment. For example, both of the following will return the current time in India.

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

or

moment().utcOffset(330).format()
like image 127
Matt Johnson-Pint Avatar answered Nov 01 '22 12:11

Matt Johnson-Pint