diff --git a/src/utils/tools.ts b/src/utils/tools.ts index 75829ee..3e0c91d 100644 --- a/src/utils/tools.ts +++ b/src/utils/tools.ts @@ -75,6 +75,26 @@ export const array = { }, }; +/** date相关工具 */ +export const date = { + /** + * 获取本地时间的ISO字符串 + * @param date {string | Date} 日期字符串或Date对象 + * @returns {string} 本地时间的ISO字符串 '2022-01-01T08:00:00.000Z' + * @example + * getLocalISOString('2022-01-01 00:00:00') // '2022-01-01T00:00:00.000Z' + * getLocalISOString(new Date('2022-01-01 00:00:00')) // '2022-01-01T00:00:00.000Z' + */ + getLocalISOString: (date: string | Date): string => { + const localDate = new Date(date); + // 获取本地时间偏移量(这里特指中国utc+8) + const offset = localDate.getTimezoneOffset() * 60000; + // 获取utc的本地时间 + const utcDate = new Date(localDate.getTime() - offset); + return utcDate.toISOString(); + }, +}; + export const generateUUID = () => { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { const r = (Math.random() * 16) | 0,