How to convert unix timestamp to a normal date / time(with seconds)
Hello, I'm writing Javascript code. The task is this-you need to translate the time from unix timestamp to a normal date/time (with seconds) and it is desirable to get each value separately(day, year, month, hour, etc.)
1
2 answers
Found on English Stackoverflow:
function timeConverter(UNIX_timestamp){
var a = new Date(UNIX_timestamp * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate();
var hour = a.getHours();
var min = a.getMinutes();
var sec = a.getSeconds();
var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
return time;
}
2
Author: Markovsky, 2017-01-02 18:13:30
You are most likely looking for Date.prototype.toLocaleString()
. Example from MDN:
let date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(date.toLocaleString('de-DE', options));
// → "Donnerstag, 20. Dezember 2012"
0
Author: Ainar-G, 2017-08-01 22:02:55