Skip to content

Commit

Permalink
fix(date-picker): 默认时区从utc修复为机器时区 (#2113)
Browse files Browse the repository at this point in the history
Co-authored-by: YanHui <[email protected]>
  • Loading branch information
hiker90 and YanHui authored Nov 11, 2022
1 parent 1f40690 commit 6bd4d64
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/static-past-time-picker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const parseTimeMode = (timeRange: string | undefined) => {
}
};

export const startOfTodayInTimezone = () => new Date(momentTZ.tz(`${(momentTZ.tz(new Date(), localStorage.getItem('timezone') || 'UTC').format()).substring(0, 10)} 00:00:00`, localStorage.getItem('timezone') || 'UTC').format());
export const startOfTodayInTimezone = () => new Date(momentTZ.tz(`${(momentTZ.tz(new Date(), localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone).format()).substring(0, 10)} 00:00:00`, localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone).format());
export const startOfYesterdayInTimezone = () => sub(startOfTodayInTimezone(), { days: 1 });

export const parseStartAndEndDate = (timeRange: string | undefined): [Date | undefined, Date | undefined] => {
Expand Down
10 changes: 5 additions & 5 deletions src/utils/timeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { format as dateFnsFormat, utcToZonedTime } from 'date-fns-tz';

// 时间日期转换时区 moment
export const parseTimeZone = (data?: any, format?: string) =>
momentTZ(data as string, format).tz(localStorage.getItem('timezone') || 'UTC');
momentTZ(data as string, format).tz(localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone);

// 时间日期转换时区 date-fns
export const parseFnsTimeZone = (date: number | Date | string, format: string) => {
Expand All @@ -14,8 +14,8 @@ export const parseFnsTimeZone = (date: number | Date | string, format: string) =
finalDate = new Date(date);
}

return dateFnsFormat(utcToZonedTime(finalDate, localStorage.getItem('timezone') || 'UTC'), format, {
timeZone: localStorage.getItem('timezone') || 'UTC',
return dateFnsFormat(utcToZonedTime(finalDate, localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone), format, {
timeZone: localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone,
});
};

Expand All @@ -25,15 +25,15 @@ export const parseFnsTimeZone = (date: number | Date | string, format: string) =
export const exportDateToZonedDate = (date: any, format?: string) => {
if (!date) return date;
return new Date(
momentTZ.tz(moment(date).format(format || 'yyyy-MM-DD HH:mm:ss'), localStorage.getItem('timezone') || 'UTC').format()
momentTZ.tz(moment(date).format(format || 'yyyy-MM-DD HH:mm:ss'), localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone).format()
);
};
// 选择器时间字符串按local时区转化
// 例: date: Fri Oct 21 2022 09:00:00 GMT+0800 (中国标准时间) format: 'yyyy-MM-DD' 浏览器TZ: 北京 localstorage: UTC;
// return Fri Oct 21 2022 01:00:00 GMT+0800 (中国标准时间)
export const exportZonedDateToDate = (date: any, format?: string) => {
if (!date) return date;
const arr = (momentTZ.tz(date, localStorage.getItem('timezone') || 'UTC').format()).split('T');
const arr = (momentTZ.tz(date, localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone).format()).split('T');

return new Date(moment(`${arr[0]} ${arr[1].substring(0, 8)}`).format(format || 'yyyy-MM-DD HH:mm:ss'));
};

0 comments on commit 6bd4d64

Please sign in to comment.