Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UNIX timestamp to date time (javascript)

Tags:

javascript

Timestamp:

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 converts the time stamp properly in terms of time format, but the date is always:

17/0/1970

Why - cheers?

like image 447
bobo2000 Avatar asked Jun 11 '14 19:06

bobo2000


1 Answers

You have to multiply by 1000 as JavaScript counts in milliseconds since epoch (which is 01/01/1970), not seconds :

var d = new Date(timestamp*1000);

Reference

like image 128
Denys Séguret Avatar answered Sep 17 '22 01:09

Denys Séguret