Skip to content

Commit

Permalink
Added getWeek
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-tackstrand committed Dec 18, 2024
1 parent 4fe3848 commit 908ce25
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions __tests__/calculations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ describe("DateTime calculations", () => {
expect(utils.getYear(date)).toBe(2018);
});

utilsTest("getWeek", (date, utils) => {
expect(utils.getWeek(date)).toBe(44);
});

utilsTest("getMonth", (date, utils) => {
expect(utils.getMonth(date)).toBe(9);
});
Expand Down
1 change: 1 addition & 0 deletions packages/core/IUtils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export interface IUtils<TDate, TLocale> {
getDate(value: TDate): number;
setDate(value: TDate, count: number): TDate;

getWeek(value: TDate): number;
getMonth(value: TDate): number;
getDaysInMonth(value: TDate): number;
setMonth(value: TDate, count: number): TDate;
Expand Down
5 changes: 5 additions & 0 deletions packages/date-fns-jalali/src/date-fns-jalali-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { formatISO } from "date-fns-jalali/formatISO";
import { getHours } from "date-fns-jalali/getHours";
import { getSeconds } from "date-fns-jalali/getSeconds";
import { getYear } from "date-fns-jalali/getYear";
import { getWeek } from "date-fns-jalali/getWeek";
import { getMonth } from "date-fns-jalali/getMonth";
import { getDate } from "date-fns-jalali/getDate";
import { getDay } from "date-fns-jalali/getDay";
Expand Down Expand Up @@ -398,6 +399,10 @@ export default class DateFnsJalaliUtils implements IUtils<Date, Locale> {
return getMinutes(date);
};

public getWeek = (date: Date) => {
return getWeek(date);
};

public getMonth = (date: Date) => {
return getMonth(date);
};
Expand Down
5 changes: 5 additions & 0 deletions packages/date-fns/src/date-fns-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { getDay } from "date-fns/getDay";
import { getDaysInMonth } from "date-fns/getDaysInMonth";
import { getHours } from "date-fns/getHours";
import { getMinutes } from "date-fns/getMinutes";
import { getWeek } from "date-fns/getWeek";
import { getMonth } from "date-fns/getMonth";
import { getSeconds } from "date-fns/getSeconds";
import { getYear } from "date-fns/getYear";
Expand Down Expand Up @@ -383,6 +384,10 @@ export default class DateFnsUtils implements IUtils<Date, Locale> {
return setDate(date, count);
};

public getWeek = (date: Date) => {
return getWeek(date);
};

public getMonth = (date: Date) => {
return getMonth(date);
};
Expand Down
6 changes: 6 additions & 0 deletions packages/dayjs/src/dayjs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import defaultDayjs, { QUnitType } from "dayjs";
import customParseFormatPlugin from "dayjs/plugin/customParseFormat";
import localizedFormatPlugin from "dayjs/plugin/localizedFormat";
import isBetweenPlugin from "dayjs/plugin/isBetween";
import weekOfYear from "dayjs/plugin/weekOfYear";
import { IUtils, DateIOFormats, Unit } from "@date-io/core/IUtils";

defaultDayjs.extend(customParseFormatPlugin);
defaultDayjs.extend(localizedFormatPlugin);
defaultDayjs.extend(isBetweenPlugin);
defaultDayjs.extend(weekOfYear);

interface Opts {
locale?: string;
Expand Down Expand Up @@ -282,6 +284,10 @@ export default class DayjsUtils<TDate extends Dayjs = Dayjs>
return date.set("second", count) as TDate;
};

public getWeek = (date: Dayjs) => {
return date.week();
};

public getMonth = (date: Dayjs) => {
return date.month();
};
Expand Down
4 changes: 4 additions & 0 deletions packages/hijri/src/hijri-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ export default class MomentUtils extends DefaultMomentUtils {
return date.iYear() > value.iYear();
};

public getWeek = (date: Moment) => {
return date.get("week");
};

public getMonth = (date: Moment) => {
return date.iMonth();
};
Expand Down
4 changes: 4 additions & 0 deletions packages/jalaali/src/jalaali-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export default class MomentUtils extends DefaultMomentUtils {
return date.jYear() > value.jYear();
};

public getWeek = (date: Moment) => {
return date.get("week");
};

public getMonth = (date: Moment) => {
return date.jMonth();
};
Expand Down
4 changes: 4 additions & 0 deletions packages/js-joda/src/js-joda-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ export default class JsJodaUtils implements IUtils<Temporal, Locale> {
return date.with(ChronoField.SECOND_OF_MINUTE, count);
}

getWeek(date: Temporal): number {
return date.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
}

getMonth(date: Temporal): number {
return date.get(ChronoField.MONTH_OF_YEAR) - 1;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/luxon/src/luxon-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ export default class LuxonUtils implements IUtils<DateTime, string> {
return value.set({ second: count });
};

public getWeek = (value: DateTime) => {
return value.get("weekNumber");
};

public getMonth = (value: DateTime) => {
// See https://github.com/moment/luxon/blob/master/docs/moment.md#major-functional-differences
return value.get("month") - 1;
Expand Down
4 changes: 4 additions & 0 deletions packages/moment/src/moment-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ export default class MomentUtils implements IUtils<defaultMoment.Moment, string>
return date.clone().seconds(count);
};

public getWeek = (date: Moment) => {
return date.get("week");
};

public getMonth = (date: Moment) => {
return date.get("month");
};
Expand Down

0 comments on commit 908ce25

Please sign in to comment.