如何通过javascript将unix纪元转换为[年、月、日]

Dea*_*pha 1 javascript

我有一个小需求,我想将存储在变量中的时间戳转换doc.creation_time为格式[year,month,day]

例子:

doc.creation_time=1537492812 这应该转换为[2018,38,Fri]

有人可以帮忙吗

小智 5

var creation_time = 1537492812;
var date = new Date(creation_time * 1000);
var weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

// UTC
console.log(date.getUTCFullYear(), date.getUTCMonth() + 1, weekday[date.getUTCDay()]);

// Local
console.log(date.getFullYear(), date.getMonth() + 1, weekday[date.getDay()]);
Run Code Online (Sandbox Code Playgroud)