update
This commit is contained in:
22
utils/date.ts
Normal file
22
utils/date.ts
Normal file
@ -0,0 +1,22 @@
|
||||
export function formatDate(dateStr:string) {
|
||||
const date = new Date(dateStr);
|
||||
|
||||
if (isNaN(date)) return 'Invalid Date';
|
||||
|
||||
// 本地时间组件
|
||||
const pad = n => String(n).padStart(2, '0');
|
||||
const year = date.getFullYear();
|
||||
const month = pad(date.getMonth() + 1);
|
||||
const day = pad(date.getDate());
|
||||
const hours = pad(date.getHours());
|
||||
const minutes = pad(date.getMinutes());
|
||||
const seconds = pad(date.getSeconds());
|
||||
|
||||
// 获取本地偏移量(分钟),转换为 +08:00 格式
|
||||
const timezoneOffset = -date.getTimezoneOffset(); // 反向处理
|
||||
const offsetHours = pad(Math.floor(Math.abs(timezoneOffset) / 60));
|
||||
const offsetMinutes = pad(Math.abs(timezoneOffset) % 60);
|
||||
const offsetSign = timezoneOffset >= 0 ? '+' : '-';
|
||||
|
||||
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}${offsetSign}${offsetHours}:${offsetMinutes}`;
|
||||
}
|
Reference in New Issue
Block a user