初始化
This commit is contained in:
24
utils/formatTime.js
Normal file
24
utils/formatTime.js
Normal file
@@ -0,0 +1,24 @@
|
||||
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; // 直接导出函数,而不是一个包含函数的对象
|
Reference in New Issue
Block a user