dayjs: Chrome v88 (88.0.4324.93) for Android does not recognize date

Device: Android phone (with Android v10) Browser: Chrome v88 (88.0.4324.93).

dayJs(‘2021-01-26T00:00:00Z’).tz(tz) returns “Invalid Date”. Started happening with Chrome upgrade v88.

dayJs(dateWithFormat).tz(tz) --> Works everywhere except Chrome88 on Android 10 dayJs.tz(dateWithFormat, tz) --> Works on Chrome 88 for Android 10.

Shouldn’t these methods return the same values irrespective of the browser or the underlying OS?

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 8
  • Comments: 22 (1 by maintainers)

Most upvoted comments

I wrote this polyfill to fix this issue. It is really ugly, but it should work.

// Chrome toLocaleString bug
(() => {
    let testDate = new Date(2021, 0, 17, 20, 16, 51);
    if (Intl?.DateTimeFormat != null) {
        let str = testDate.toLocaleString('en-US');
        if (str.indexOf('PM') == -1 || Number.isNaN(new Date(str).valueOf())) {
            if (new Intl.DateTimeFormat('cs-CZ').formatToParts != null) {


                let original = Date.prototype.toLocaleString;
                Date.prototype.toLocaleString = function (locale, opts) {
                    if (locale != 'en-US' || opts == null || Object.keys(opts).length != 1) {
                        return original.call(this, locale, opts);
                    }

                    let format = new Intl.DateTimeFormat('cs-CZ', {
                        year: 'numeric',
                        month: 'numeric',
                        day: 'numeric',
                        hour: 'numeric',
                        minute: 'numeric',
                        second: 'numeric',
                        hour12: false,
                        timeZone: opts.timeZone
                    });
                    let parts = format.formatToParts(this);
                    let component = (type) => {
                        return parseInt(parts.filter(x => x.type == type)[0].value, 10);
                    };
                    return new Date(component('year'), component('month') - 1, component('day'), component('hour'), component('minute'), component('second')).toISOString();
                };
            }
        }
    }
})();

Notice: This code is only usable if you do not use “toLocaleString” anywhere, except dayjs. It just fix dayjs, but can break other code (if it use toLocaleString method).

Looks like chrome started to ignore locale in arguments ECMA-402 image