22 lines
863 B
TypeScript
22 lines
863 B
TypeScript
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}`;
|
|
} |