1

I am getting the time stamp from a server in following format

"Wed Jan 15 14:17:06 PST 2014"

or

"Wed Jan 15 14:17:06 IST 2014"

when I am doing getTime on second timestamp having IST I am getting NaN . Please take a look below:

when I do:

var str = "Wed Jan 15 14:17:06 PST 2014";
var output = new Date(str).getTime();

i get output "1389824226000" which is expected

But when i do

var str   = "Wed Jan 15 14:17:06 IST 2014";
var output = new Date(str).getTime();

I get output as NaN.

How can i make getTime work of IST(India Standard Time) also.

Marriott81
  • 275
  • 2
  • 16
druveen
  • 1,611
  • 3
  • 15
  • 32

1 Answers1

5

Time zone abbreviations are ambiguous an inconsistent. IST could be either Indian Standard Time, Irish Standard Time or Israel Standard Time.

The only reason that PST works is because it is specifically called out in the RFC 822 spec (section 5.1), along with a few others for North America. And that spec is slowly fading away in favor of ISO 8601, which does not use time zone abbreviations at all.

So basically - you shouldn't be parsing a string in this format - ever. If you feel you need to for some reason then you have an XY Problem. Take a step back and explain your reasoning, because there is probably a better approach.

Updated Answer

In your comments you said it is out of your control to change the server's output. Well, then you really should go find whomever has it within their power to make changes to the server and talk to them. It's very important that server APIs expose their data in a manner that is standards compliant and easy to consume. Since the server is exposing what is essentially a made up format, then consumers of the API (like yourself) will have trouble working with the data.

And yes, it is a "made up" format. There is no standard for date and time that includes IST as a supported time zone abbreviation.

There are other ways to deal with this, but these are hacks and not a recommended approach:

  • You could remove "IST" from the string, and append "+0530". Most browsers should pick up on that.

    new Date(str.replace('IST','') + ' +0530')
    
  • You could parse it with a library like moment.js:

    var s = str.replace('IST','+05:30');
    var m = moment.parseZone(s,"ddd MMM DD HH:mm:ss Z YYYY");
    
    var output = moment.toDate(); // if you need a js Date object
    var output = moment.format("whatever"); // if you want to reformat a string
    

Both of these are truly hacks, because they rely on two uncertainties:

  • That you will only ever use IST coming from your server, and it will never run somewhere with a different time zone.

  • That IST is fixed to +05:30 and always will be. Many time zones go through changes based on the whim of politicians, and many change offsets twice annually for daylight saving time. IST does not use DST, so you can exploit that here.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Ok i got it now so i have to convert inconsistent time zone like IST to some standard timezone like PST .... and the use it right? – druveen Jan 15 '14 at 09:21
  • No. You should not be using time zone abbreviations in input. Period. What are you actually trying to achieve?? – Matt Johnson-Pint Jan 15 '14 at 09:25
  • 1
    Start with your very first sentence "I am getting the time stamp from a server in following format...". Stop there. Go back to your server and use a different format, preferably an ISO 8601 formatted timestamp like `2014-01-15T01:23:45+05:30` – Matt Johnson-Pint Jan 15 '14 at 09:30
  • Sorry to say but that is not under my control. what i want to achive is number of milliseconds between midnight of January 1, 1970 to time stamp i get from server – druveen Jan 15 '14 at 09:48