Files
pasd_app/utils/formatTime.js
2025-07-28 14:57:35 +08:00

24 lines
883 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function formatTime(dateTimeStr) {
// 将字符串解析为Date对象
const date = new Date(dateTimeStr);
// 验证日期是否有效
if (isNaN(date.getTime())) {
throw new Error('Invalid date string');
}
// 获取各个时间部分
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的
const day = String(date.getDate()).padStart(2, '0');
let hours = date.getHours();
const minutes = String(date.getMinutes()).padStart(2, '0');
// 将24小时制转换为12小时制并处理午夜情况
hours = hours === 0 ? 12 : hours % 12;
// 拼接字符串
return `${year}${month}${day}${hours}:${minutes}`;
}
export default formatTime; // 直接导出函数,而不是一个包含函数的对象