Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tmkx opened this issue Oct 12, 2023 · 2 comments
Closed

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

tmkx opened this issue Oct 12, 2023 · 2 comments
Labels

Comments

@tmkx
Copy link
Contributor

tmkx commented Oct 12, 2023

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');
  }
@sslincoco
Copy link

sslincoco commented Oct 12, 2023 via email

gilmoreorless added a commit that referenced this issue Dec 29, 2023
@gilmoreorless
Copy link
Member

Thanks for the detailed report. I've merged #1082 which contains the fix, and added your example as a test case in ece926a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants