dayjs
dayjs copied to clipboard
CustomFormat with setDefault() Timezone does not work
Describe the bug Using customFormat with timezone plugin and setting a defaultTimezone does not work. Without a TimeZone, Safari struggles with the correct unix timestamp.
console.log(dayjs("02.04.1971", "DD.MM.YYYY").unix()); // Chrome: 39394800 Safari on MacOS or iOS: 39391200
so we tried using timezone, see Expected behavior.
Expected behavior Current Setup:
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(customParseFormat);
dayjs.tz.setDefault("Europe/Berlin");
console.log(dayjs.tz("02.04.1971", "DD.MM.YYYY", "Europe/Berlin").unix()); // correct timestamp in both browsers: 39394800
console.log(dayjs.tz("02.04.1971", "DD.MM.YYYY").unix()); // RangeError: invalid time zone: DD.MM.YYYY
console.log(dayjs.tz("1971/04/02").unix()); // correct timestamp in both browsers
Information
- Day.js Version 1.9.5
- OS: MacOS
- Browser Chrome 86, Safari 14. Unix Timestamp bug reproducible in all iOS devices
- Time zone: UTC+1 Europe/Berlin
First, thanks @iamkun for the added third argument in the first place! As noted in the report, the argument parsing currently leaves no option to use the default timezone:
https://github.com/iamkun/dayjs/blob/34cfb920b9653ad44d4b31fe49e533692a3ce01b/src/plugin/timezone/index.js#L117-L119
I think when enhancing this it should also be possible to add a locale / strict parsing, as CustomParseFormat
does. Working around both limitations currently looks like this, as there is also no getDefaultTimezone
:
const date = dayjs.tz(
'17.11.2020',
dayjs().locale('de').$locale().formats.L,
dayjs.tz().$x.$timezone
);
Yup, same problem.
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(customParseFormat)
dayjs.tz.setDefault('Europe/Paris')
const formatted = dayjs.tz(input_value, 'DD/MM/YYYY')
RangeError: invalid time zone in DateTimeFormat(): DD/MM/YYYY
I noticed the typings:
(date: ConfigType, format: string, timezone?: string): Dayjs
suggest, that I would be able to just use undefined
as the timezone to be able to use the default one, however this leads to the same issue of it trying to parse the format as the timezone.
I have the same issue
d.tz = function (input, arg1, arg2) { const parseFormat = arg2 && arg1 const timezone = arg2 || arg1 || defaultTimezone
Why is 'defaultTimezone' at the very end? if put 'defaultTimezone' at the very beginning that should be able to solve this problem.