Skip to content

Commit

Permalink
ISO year was calculate depending on ISO 8601 (#431)
Browse files Browse the repository at this point in the history
* ISO year was calculate depending on ISO 8601

* getIsoYearByDate and getIsoYear test cases added
  • Loading branch information
lash1co authored Dec 31, 2024
1 parent 6576354 commit 6374bd3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/dateRange.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
getIsoYear,
getWeekIsoNumber,
getWeekIsoOfYear,
getWeekNumber,
Expand Down Expand Up @@ -273,7 +274,7 @@ export class YearWeek extends DateRange implements IYearWeekDateRange {

export class YearWeekIso extends DateRange implements IYearWeekDateRange {
constructor(year?: number, week?: number) {
const actualYear = year ?? new Date().getFullYear();
const actualYear = year ?? getIsoYear();
const actualWeek = week ?? getWeekIsoOfYear();

const startDate = new Date(actualYear, 0, 1 + (actualWeek - 1) * 7);
Expand Down
9 changes: 9 additions & 0 deletions src/weekUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ export const getWeekIsoNumber = (date: Date): number => {
};

export const getWeekIsoOfYear = (): number => getWeekIsoNumber(new Date());

export const getIsoYearByDate = (date: Date): number => {
const dayOfWeek = date.getDay() === 0 ? 7 : date.getDay();
const thursday = new Date(date);
thursday.setDate(date.getDate() + (4 - dayOfWeek));
return thursday.getFullYear();
};

export const getIsoYear = () : number => getIsoYearByDate(new Date());
18 changes: 18 additions & 0 deletions test/weekUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
getIsoYear,
getIsoYearByDate,
getWeekIsoNumber,
getWeekIsoOfYear,
getWeekNumber,
Expand Down Expand Up @@ -31,4 +33,20 @@ describe("weekUtils", () => {
expect(getWeekIsoOfYear()).toBe(currentWeekNumber);
});
});

describe("getIsoYear", () => {
it("should return the Iso year number of the current date", () => {
const currentDate = new Date();
const isoYearNumber = getIsoYearByDate(currentDate);
expect(getIsoYear()).toBe(isoYearNumber);
});
});

describe("getIsoYearByDate", () => {
it("should return the Iso year number of an specific date", () => {
const specificDate = new Date(2024,12,31);
const isoCorrectYear = 2025;
expect(getIsoYearByDate(specificDate)).toBe(isoCorrectYear);
});
});
});

0 comments on commit 6374bd3

Please sign in to comment.