如何将JavaScript中的Epoch时间转换为本地时间



我仍在学习javascript,所以我不知道如何使用GMT+7(我当前所在的时区(将epoch时间转换为标准时间格式。

如何将其更改为所需时区?

我在这里有我的源代码:

// convert time to human-readable format YYYY/MM/DD HH:MM:SS
function epochToDateTime(epochTime){
var epochDate = new Date(epochToJsDate(epochTime));
var dateTime = epochDate.getFullYear() + "/" +
("00" + (epochDate.getMonth() + 1)).slice(-2) + "/" +
("00" + epochDate.getDate()).slice(-2) + " " +
("00" + epochDate.getHours()).slice(-2) + ":" +
("00" + epochDate.getMinutes()).slice(-2) + ":" +
("00" + epochDate.getSeconds()).slice(-2);

return dateTime;
}

这就是您需要的吗?

const now = new Date(Date.now())
const dd = now.getDate()
const mm = now.getMonth()
const yyyy = now.getFullYear()
const HH = now.getHours()
const MM = now.getMinutes()
const SS = now.getSeconds()
console.log(yyyy + '/' + mm + '/' + dd + ' ' + HH + ':' + MM + ':' + SS)

最新更新