Ankush Ankush - 1 year ago 151
Javascript Question

Convert Javascript Date object to PST time zone

I need to pick a future date from calender, suppose the date I am selecting is

, now what I want is to send the date with the time to server, so that at server end it always reaches as 6am time in PST timezone and the format of date should be UTC.

What I am doing is

targetDate = new Date($("#calendar").val());
targetDate = targetDate.toUTCString();
targetDate = targetDate.addHours(14);

My understanding is that PST timezone is -8:00 so I have added 14 hours to the UTC time so that time becomes 6:00am PST

The problem I am facing is that it is not letting me to add 14 hours since the object has already been converted to string.

addHours is the custom function I am having to add the hours in given time.

If I write

targetDate = new Date($("#calendar").val());
targetDate = targetDate.addHours(14);
targetDate = targetDate.toUTCString();

then it works good but in this case problem is time will always be different when the request is coming from different timezones.

Any help is appreciated.

Answer Source

Suggest you look at DateJS or Handles PDT for you.

Here is an alternative for you:

Use: Date.UTC(year, month, day, hours, minutes, seconds, ms)

Example: For 1 Jan 2013 6AM PST

var date = new Date(Date.UTC(2013, 0, 1, 14, 0, 0))

Prints: "Tue, 01 Jan 2013 14:00:00 GMT"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download