2023-02-21 22:34:08 +08:00

63 lines
1.1 KiB
JavaScript

const formateDate = timestamp => {
if (!timestamp) {
return;
}
let date = new Date(parseInt(timestamp));
let year = date.getFullYear(); // 年
let month = date.getMonth() + 1; // 月
let day = date.getDate(); // 日
let hour = date.getHours(); // 时
let minutes = date.getMinutes(); // 分
let seconds = date.getSeconds(); // 秒
let weekArr = [
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
];
let week = weekArr[date.getDay()];
// 需要给一位数前面加 0
// 9 点 ----> 09:45:03
// if (month >= 1 && month <= 9) {
// // month += '0'; // a += b ----> a = a + b
// month = "0" + month;
// }
if (day >= 0 && day <= 9) {
day = "0" + day;
}
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds;
}
return {
year,
month,
day,
hour,
minutes,
seconds,
week
};
}
module.exports = {
formateDate
}