Repository navigation
fix(#114): don't lose the day of a date that is set in the datetime helper - #188
Open
muditagrawal2007 wants to merge 1 commit into
Open
muditagrawal2007 wants to merge 1 commit into
muditagrawal2007 wants to merge 1 commit into
Conversation
…time helper
The datetime helper built the date it was given by setting the day, the
month and the year one after the other on the current date:
now.set("date", parsedDate.date);
now.set("month", parsedDate.month);
now.set("year", parsedDate.year);
Setting the day first overflows into the next month when the current month
is shorter than the day that was asked for, and the day is then silently
lost. The 31st of December set while the current month is November became
the 1st of December, and every date calculated from it was 30 days off.
The user reported a deadline of 31/12/2025 producing a mid project review
of 17/11/2025 and a project closure of 08/12/2025, which is exactly what
this produced on 30 November 2025.
src/helpers/datetime.ts
- Set the day to 1 before changing the month and the year, and set the
day last, so that the date that was asked for is kept.
src/utils/dateAndTime.ts
- Documented that parseDate() returns a zero based month, the way moment
handles it.
tests/helpers/datetime.spec.ts
- Added tests for the datetime helper, including the reported case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #114
The bug
The
datetimehelper builds the date it is given by setting the day, the month and the year one after the other on the current date:Setting the day first overflows into the next month when the current month is shorter than the day that was asked for, and the day is then silently lost. November has 30 days, so the 31st of December became the 1st of December, and every date calculated from it was 30 days off.
The dates in the report are exactly what this produced on 30 November 2025:
The bug only shows up while the current month is shorter than the day that is set, which is why it can go unnoticed for months at a time.
Before / after
A deadline of the 31st of December 2025, with the note created on the 30th of November 2025:
After the fix the dates are calculated from the deadline that was entered:
Both images are rendered from the real production code, with the
DD/MM/YYYYdate format.The fix
src/helpers/datetime.tsThe day is set to 1 before the month and the year are changed, and the day that was asked for is set last:
With the day at 1, changing the month or the year can't overflow, and setting the day last can't overflow either, because the date that is parsed always exists in the month it belongs to.
src/utils/dateAndTime.tsDocumented that
parseDate()returns a zero based month, the way moment handles it, so that the result can be passed to moment'sset("month", ...)directly. No behaviour change.Tests
tests/helpers/datetime.spec.tsis a new file with tests for thedatetimehelper, including the reported case and a template that calculates its dates from a deadline.Against the old implementation 11 of the 13 new tests fail:
The other two are guards for the cases that already worked.
Testing
npm test— 114 tests pass.npm run lint— no new warnings.npm run dist— the plugin builds.DEMO_TEMPLATE.md, enter a deadline at the end of a month and check that the calculated dates are 14 days before and 7 days after it.