feat: fix timezone
This commit is contained in:
parent
1de9a1f605
commit
326c1b60c6
|
|
@ -6,22 +6,12 @@ import utc from 'dayjs/plugin/utc';
|
|||
dayjs.extend(timezone);
|
||||
dayjs.extend(utc);
|
||||
|
||||
const {utc: originalUtc} = dayjs;
|
||||
|
||||
dayjs.utc = new Proxy(originalUtc, {
|
||||
apply(target, thisArg, args) {
|
||||
const result = target.apply(thisArg, args);
|
||||
|
||||
// Attach `.local()` method to the returned Dayjs object
|
||||
result.local = function () {
|
||||
return result.tz(getTimezone());
|
||||
};
|
||||
|
||||
return result;
|
||||
},
|
||||
});
|
||||
const { utc: originalUtc } = dayjs;
|
||||
|
||||
export const getTimezone = () => {
|
||||
if (typeof window === 'undefined') {
|
||||
return dayjs.tz.guess();
|
||||
}
|
||||
return localStorage.getItem('timezone') || dayjs.tz.guess();
|
||||
};
|
||||
|
||||
|
|
@ -31,6 +21,18 @@ export const newDayjs = (config?: ConfigType) => {
|
|||
|
||||
const SetTimezone: FC = () => {
|
||||
useEffect(() => {
|
||||
dayjs.utc = new Proxy(originalUtc, {
|
||||
apply(target, thisArg, args) {
|
||||
const result = target.apply(thisArg, args);
|
||||
|
||||
// Attach `.local()` method to the returned Dayjs object
|
||||
result.local = function () {
|
||||
return result.tz(getTimezone());
|
||||
};
|
||||
|
||||
return result;
|
||||
},
|
||||
});
|
||||
if (localStorage.getItem('timezone')) {
|
||||
dayjs.tz.setDefault(getTimezone());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue