Skip to content

Instantly share code, notes, and snippets.

@alexburner
Created November 29, 2011 19:55
Show Gist options
  • Save alexburner/1406189 to your computer and use it in GitHub Desktop.
Save alexburner/1406189 to your computer and use it in GitHub Desktop.
Turning PST / PDT unixtime stamps into UTC date objects
// unixtime stamps from server are PT, need to make them UTC
var getAdjustedDate = function ( unixtime ) {
if ( !unixtime ) { return new Date(); }
var date, dst, offset;
// create date object
if ( typeof unixtime === 'number' ) {
date = new Date( unixtime * 1000 );
} else if ( typeof unixtime === 'string' ) {
unixtime = parseInt( unixtime, 10 );
date = new Date( unixtime * 1000 );
} else {
return new Date();
}
// try to figure out if client is in daylight savings time
date = date.toString();
dst = date.match( /\((.*)\)/ );
dst = dst && dst.length ? dst[ 1 ][ 1 ] : null;
// assume that if they are, pacific zone will be as well
offset = dst === 'D' ? 7 * 60 * 60 : 8 * 60 * 60;
// subtract pacific offset to make unixtime match UTC
unixtime = unixtime - offset;
// return the adjusted date
return new Date( unixtime * 1000 );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment