I am having problem using Date Object, when I try to parse unix-timestamp.
example:
//I am receiving json data that contains Unix-Timestamp like 1348514100000
var unixTimestamp = data.timestamp; //1348514100000
var date = new Date(unixTimestamp);
console.log(date.getDay()); // is wrong..
and example 2:
var unixTimestamp = data.timestamp; //1348514100000
var date = new Date();
date = date.setDate(unixTimestamp);
console.log(date.getDay()); // still wrong..
I have tried to parse string-timestamp to convert int with
var intUT = parseInt(unixTimestamp);
but still not the correct date&time.. I have really no idea what to do. How can I get the correct date and time from this unix timestamp?
Thank you!
Your code is completely fine, the only issue might be with incorrect type. Trying this:
gives me
Mon Sep 24 2012 21:15:00 GMT+0200 (CEST), others reportedMon Sep 24 2012 14:15:00 GMT-0500 (CDT)and12:15:00 GMT-0700 (PDT). According to http://www.epochconverter.com it isMon, 24 Sep 2012 19:15:00 GMT.Note that all these dates represent exactly the same point in time. The time zone difference (and thus different hour) is only due to stupid
Date.toString()implementation (mistake copied from Java language).The only portable and built-in way to deal with such dates is to either operate on epoch time (milliseconds) or use
getUTC*()family of methods:The code above will always return 19 (GMT time), irrespective to browser/OS time zone.