Convert UNIX timestamp to date (javascript)

Mark:

1395660658

code:

//timestamp conversion
exports.getCurrentTimeFromStamp = function(timestamp) {
    var d = new Date(timestamp);
    timeStampCon = d.getDate() + '/' + (d.getMonth()) + '/' + d.getFullYear() + " " + d.getHours() + ':' + d.getMinutes();

    return timeStampCon;
};

This correctly converts the timestamp in terms of time format, but the date is always:

17/0/1970

Why greetings?

+1
source share
2 answers

You should multiply by 1000 since the JavaScript counter is in milliseconds from the era (this is 01/01/1970), not seconds:

var d = new Date(timestamp*1000);

Reference

+6
source

Because your time is in seconds. Javascript requires it to be in milliseconds since the era. Multiply it by 1000, and that should be what you want.

//time in seconds
var timeInSeconds = ~(new Date).getTime();
//invalid time
console.log(new Date(timeInSeconds));
//valid time
console.log(new Date(timeInSeconds*1000));
+2
source

Source: https://habr.com/ru/post/1615528/


All Articles