Ash
Ash

Reputation: 237

Converting Unix Timestamp to PST

Hi I have a json which I am getting some data. There the time format I am getting is like

1367023443000

I want to convert this to the Normal PST format. Ive tried using Javascript`s Date method. Passed the Unix time to the Date method,

var now = new Date(1367023443000); 

I am getting only IST value, But not PST. What should I do here to convert the Unix timestamp to PST?

Upvotes: 0

Views: 6311

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241563

If you're not actually in the US Pacific Time zone, the only way to do this reliably in JavaScript is with a library that implements the TZDB database. I list several of them here.

For example, using walltime-js library, you can do the following:

var date = new Date(1367023443000);
var pacific = WallTime.UTCToWallTime(date, "America/Los_Angeles");
var s = pacific.toDateString() + ' ' + pacific.toFormattedTime();

// output:  "Fri Apr 26 2013 5:44 PM"

You can't just add or subtract a fixed number, because the target time zone may use a different offset depending on exactly what date you're talking about. This is primarily due to Daylight Saving Time, but also because time zones have changed over time.

Upvotes: 2

Related Questions