Skip to content

Commit

Permalink
fix(DateTime): correctly calculate start of year (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS authored Jan 2, 2024
1 parent 7534665 commit c902937
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/dateTime/__tests__/startEndOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {dateTime} from '../dateTime';

test('start of year', () => {
const m = dateTime({input: new Date(2011, 1, 2, 3, 4, 5, 6)}).startOf('year');
const ms = dateTime({input: new Date(2011, 1, 2, 3, 4, 5, 6)}).startOf('years');
const ma = dateTime({input: new Date(2011, 1, 2, 3, 4, 5, 6)}).startOf('y');
const ms = dateTime({input: new Date(2011, 7, 2, 3, 4, 5, 6)}).startOf('years');
const ma = dateTime({input: new Date(2011, 11, 2, 3, 4, 5, 6)}).startOf('y');

expect(Number(m)).toBe(Number(ms)); // 'Plural or singular should work'
expect(Number(m)).toBe(Number(ma)); // 'Full or abbreviated should work'
Expand All @@ -18,8 +18,8 @@ test('start of year', () => {

test('end of year', () => {
const m = dateTime({input: new Date(2011, 1, 2, 3, 4, 5, 6)}).endOf('year');
const ms = dateTime({input: new Date(2011, 1, 2, 3, 4, 5, 6)}).endOf('years');
const ma = dateTime({input: new Date(2011, 1, 2, 3, 4, 5, 6)}).endOf('y');
const ms = dateTime({input: new Date(2011, 7, 2, 3, 4, 5, 6)}).endOf('years');
const ma = dateTime({input: new Date(2011, 11, 2, 3, 4, 5, 6)}).endOf('y');

expect(Number(m)).toBe(Number(ms)); // 'Plural or singular should work'
expect(Number(m)).toBe(Number(ma)); // 'Full or abbreviated should work'
Expand Down
9 changes: 6 additions & 3 deletions src/dateTime/dateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ class DateTimeImpl implements DateTime {
/* eslint-disable no-fallthrough */
switch (unit) {
case 'year':
dateComponents.month = 0;
case 'quarter':
dateComponents.month = this.month() - (this.month() % 3);
if (unit === 'quarter') {
dateComponents.month = this.month() - (this.month() % 3);
} else {
dateComponents.month = 0;
}
case 'month':
case 'weekNumber':
case 'isoWeekNumber':
Expand Down Expand Up @@ -345,7 +348,7 @@ class DateTimeImpl implements DateTime {
} else {
mixed = {...dateComponents, ...newComponents};

if (newComponents.day === undefined) {
if (newComponents.date === undefined) {
mixed.date = Math.min(daysInMonth(mixed.year, mixed.month), mixed.date);
}
}
Expand Down

0 comments on commit c902937

Please sign in to comment.