Skip to content

Passing array value to format param to dayjs.utc() silently disables utc mode #3013

Description

@suheb

Describe the bug
When an array is passed as the format argument to dayjs.utc(), UTC mode is not used: the date is parsed in the local timezone.
dayjs() accepts an array for the format parameter (with the customParseFormat plugin), so it’s easy to assume dayjs.utc() does too. With an array, dayjs.utc() does not throw; it silently parses in local time, which can cause subtle bugs (e.g. tests passing only when TZ=UTC).

To reproduce

const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const customParseFormat = require('dayjs/plugin/customParseFormat');

dayjs.extend(utc);
dayjs.extend(customParseFormat);

// With a single format, result is UTC midnight (correct)
const single = dayjs.utc('1995-05-01', 'YYYY-MM-DD');
console.log(single.toISOString()); // '1995-05-01T00:00:00.000Z'

// With a format array, result is local midnight (incorrect)
const withArray = dayjs.utc('1995-05-01', ['YYYY-MM-DD', 'YYYY-M-D']);
console.log(withArray.toISOString()); // e.g. '1995-05-01T23:00:00.000Z' in UTC+1

Stackblitz link https://stackblitz.com/edit/dayjs-playground-8cua8kja?file=index.js

Expected behavior
Either:

  • Support format arrays and parse in UTC (same semantics as single-format dayjs.utc(date, format)), or
  • Reject array format (e.g. throw error)

Likely cause
In customParseFormat, when format is an array, the code uses d.apply(this, args) (the default constructor) instead of the UTC constructor, so each format is tried in local time.

Information

  • Day.js Version 1.11.13
  • OS: macos
  • Browser: NA
  • Time zone: NA

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions