feat: fix timezone

This commit is contained in:
Nevo David 2025-08-07 20:50:07 +07:00
parent 1de9a1f605
commit 326c1b60c6
1 changed files with 16 additions and 14 deletions

View File

@ -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());
}