0

I am getting a server date (including timezone) from an API. I want to convert it to date object.

When I do this

var demo2 = new Date("THU JAN 19 15:37:56 PST 2017"); //It works!

var demo = new Date("FRI JAN 20 07:50:07 SGT 2017"); //Invalid date

Why? If it's a SGT then it doesn't work but it works for PST

I'm trying to pass this date to dateTimePicker

    $datetimepicker.datetimepicker({
        minDate: new Date(this.serverTime),
        defaultDate: new Date(this.serverTime),
    });

How can I solve this problem using Date / MomentJS?

TechnoCorner
  • 4,879
  • 10
  • 43
  • 81
  • 1
    You don't have a time zone. You have a few letters that might refer to an abbreviation of one of several time partial time zones. If your string from your server is indeed like that - you must go back to your server and emit valid output, preferably in ISO8601 format. – Matt Johnson-Pint Jan 20 '17 at 00:59

3 Answers3

1

In general, you should not ever parse time zone abbreviations. They are inconsistent at best, and often ambiguous.

For example, should CST mean...

  • Central Standard Time (UTC-6)
  • Cuba Standard Time (UTC-5)
  • China Standard Time (UTC+8)

Do I need to say more?

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

If you are using MomentJS the constructor suggest you can do something like this. (Date String, Date Format, TimeZone)

You can find the timezone strings here: http://momentjs.com/timezone/

In your case you are looking for Singapore which is: Asia/Singapore

Simple example:

var date = moment.tz("Jay 20th 2017 8PM", "MMM Do YYYY hA", "Asia/Singapore");

Here is a link to the constructor docs: http://momentjs.com/timezone/docs/

mdbox
  • 412
  • 5
  • 12
-1

Did you try using this format:

new Date('1/20/2017 7:50:07 AM CST');

CST is the same time zone than SGT

rochastuff
  • 125
  • 3
  • 8