Skip to content

moment().valueOf returns a value even though it's invalid #1075

@tmkx

Description

@tmkx

Environment

moment.tz.setDefault();
t.ok(
  Number.isNaN(moment("2023/10/06", ["H", "HH", "HH:mm"], true).valueOf()),
);

moment.tz.setDefault('America/New_York');
// not expected
t.ok(
  Number.isNaN(moment("2023/10/06", ["H", "HH", "HH:mm"], true).valueOf()),
);

Issue description

when defaultZone is specified, valueOf will return a value even though it's invalid.

it seems to be caused by:

if (zone && needsOffset(mom) && !mom._isUTC) {
mom._d = moment.utc(mom._a)._d;
mom.utc().add(zone.parse(mom), 'minutes');
}

a fix that works:

- if (zone && needsOffset(mom) && !mom._isUTC) { 
+ if (zone && needsOffset(mom) && !mom._isUTC && mom.isValid()) { 
    mom._d = moment.utc(mom._a)._d;
    mom.utc().add(zone.parse(mom), 'minutes');
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions