Skip to content

Instantly share code, notes, and snippets.

@freekrai
Created October 12, 2016 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freekrai/f37b1885125d3eeaddb07b4ba2279228 to your computer and use it in GitHub Desktop.
Save freekrai/f37b1885125d3eeaddb07b4ba2279228 to your computer and use it in GitHub Desktop.
convert timestamps
function toUserTimeZone(utc_date, offset){
if (utc_date.slice(-3) !== "UTC") {
utc_date += " UTC";
}
var local_date = new Date(utc_date);
var local_offset = local_date.getTimezoneOffset() * 60000;
var utc_time = local_date.getTime() + local_offset;
return new Date(utc_time + (3600000 * offset));
}
var date_string = "2014/08/12 16:32:28";
var vet_date = toUserTimeZone(date_string, -4.5);
var brt_date = toUserTimeZone(date_string, -3);
console.log(vet_date); // Tue Aug 12 2014 12:02:28 GMT-0430 (VET)
console.log(brt_date); // Tue Aug 12 2014 13:32:28 GMT-0430 (VET)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment