Ankush Ankush - 27 days ago 23
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

10/14/2014
, 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

Suggest you look at DateJS http://code.google.com/p/datejs/ or http://www.datejs.com/. 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))
console.log(date.toUTCString());

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