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

Dayjs TZ add 3 months to generate date with day becoming 1st of month #1563

Open
mattiLeBlanc opened this issue Jul 4, 2021 · 1 comment

Comments

@mattiLeBlanc
Copy link

When using a date object created with tz, adding 3 months if not setting the day of the month correctly.
When I revert back to using dayjs() it outputs 4th of October.

dayjs.tz.setDefault('Australia/Sydney');

const today = dayjs().tz();
console.log('TODAY', today.format()); // => 4th of July 2021
console.log('ADD 3 Months', today.add(3, 'month'));  // =>  1th of October

Expected behavior

console.log('TODAY', today.format()); // => 4th of July 2021
console.log('ADD 3 Months', today.add(3, 'month'));  // =>  4th of October

Information

  • Day.js Version 1.10.5
  • OS: MacOS
  • Node 14
  • Time zone: [Sydney/Australia]
@mattiLeBlanc
Copy link
Author

Strangely, adding 1 or 2 months works fine:

  const today = dayjs();
  console.log('TODAY', today.format());
  console.log('ADD 2 Months', today.add(2, 'month'));
  const today2 = dayjs().tz();
  console.log('TODAY', today2.format());
  console.log('ADD 2 Months', today2.add(2, 'month'));

output:

 console.log
    TODAY 2021-07-04T13:03:24+10:00

      at src/functions/processInvestment/test/process-investment.test.ts:31:11

  console.log
    ADD 2 Months d {
      '$L': 'en',
      '$d': 2021-09-04T03:03:24.704Z,
      '$x': {},
      '$y': 2021,
      '$M': 8,
      '$D': 4,
      '$W': 6,
      '$H': 13,
      '$m': 3,
      '$s': 24,
      '$ms': 704
    }

      at src/functions/processInvestment/test/process-investment.test.ts:32:11

  console.log
    TODAY 2021-07-04T13:03:24+10:00

      at src/functions/processInvestment/test/process-investment.test.ts:34:11

  console.log
    ADD 2 Months d {
      '$L': 'en',
      '$offset': 600,
      '$d': 2021-09-04T03:03:24.731Z,
      '$x': { '$timezone': 'Australia/Sydney' },
      '$y': 2021,
      '$M': 8,
      '$D': 4,
      '$W': 6,
      '$H': 13,
      '$m': 3,
      '$s': 24,
      '$ms': 731
    }

but going to 3+ months it fails:

console.log
    TODAY 2021-07-04T13:03:59+10:00

      at src/functions/processInvestment/test/process-investment.test.ts:31:11

  console.log
    ADD 3 Months d {
      '$L': 'en',
      '$d': 2021-10-04T02:03:59.174Z,
      '$x': {},
      '$y': 2021,
      '$M': 9,
      '$D': 4,
      '$W': 1,
      '$H': 13,
      '$m': 3,
      '$s': 59,
      '$ms': 174
    }

      at src/functions/processInvestment/test/process-investment.test.ts:32:11

  console.log
    TODAY 2021-07-04T13:03:59+10:00

      at src/functions/processInvestment/test/process-investment.test.ts:34:11

  console.log
    ADD 3 Months d {
      '$L': 'en',
      '$offset': 600,
      '$d': 2021-10-01T03:03:59.201Z,
      '$x': { '$timezone': 'Australia/Sydney' },
      '$y': 2021,
      '$M': 9,
      '$D': 1,
      '$W': 5,
      '$H': 13,
      '$m': 3,
      '$s': 59,
      '$ms': 201
    }

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

No branches or pull requests

1 participant