33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
// src/utils/format-date.js
|
|
|
|
export function dateToUnix(dateString) {
|
|
const date = new Date(dateString);
|
|
return Math.floor(date.getTime() / 1000);
|
|
}
|
|
|
|
export function unixToDate(timestamp) {
|
|
const date = new Date(timestamp * 1000);
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
const day = String(date.getDate()).padStart(2, "0");
|
|
return `${year}-${month}-${day}`;
|
|
}
|
|
|
|
export function formatDateTime(unixTimestamp) {
|
|
// 如果时间戳不存在或为0,返回'未知'
|
|
if (!unixTimestamp) return "未知";
|
|
|
|
// 将Unix时间戳转换为毫秒
|
|
const date = new Date(unixTimestamp * 1000);
|
|
|
|
// 获取日期和时间的各个部分
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
const day = String(date.getDate()).padStart(2, "0");
|
|
const hours = String(date.getHours()).padStart(2, "0");
|
|
const minutes = String(date.getMinutes()).padStart(2, "0");
|
|
|
|
// 返回格式化的日期时间字符串
|
|
return `${year}-${month}-${day} ${hours}:${minutes}`;
|
|
}
|