diff --git a/README.md b/README.md index 9e08c1d3..8ec9ae96 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,8 @@ export interface IUtils { isWithinRange(value: TDate, range: [TDate, TDate]): boolean; + startOfYear(value: TDate): TDate; + endOfYear(value: TDate): TDate; startOfMonth(value: TDate): TDate; endOfMonth(value: TDate): TDate; startOfWeek(value: TDate): TDate; diff --git a/__tests__/calculations.test.ts b/__tests__/calculations.test.ts index 33c452b0..d7df795a 100644 --- a/__tests__/calculations.test.ts +++ b/__tests__/calculations.test.ts @@ -66,6 +66,18 @@ describe("DateTime calculations", () => { ); }); + utilsTest("startOfYear", (date, utils, lib) => { + expect(utils.formatByString(utils.startOfYear(date), formats.dateTime[lib])).toBe( + "2018-01-01 00:00" + ); + }); + + utilsTest("endOfYear", (date, utils, lib) => { + expect(utils.formatByString(utils.endOfYear(date), formats.dateTime[lib])).toBe( + "2018-12-31 23:59" + ); + }); + utilsTest("startOfMonth", (date, utils, lib) => { expect(utils.formatByString(utils.startOfMonth(date), formats.dateTime[lib])).toBe( "2018-10-01 00:00" diff --git a/__tests__/date-fns-jalali.test.ts b/__tests__/date-fns-jalali.test.ts index d1cfe066..c7fb1388 100644 --- a/__tests__/date-fns-jalali.test.ts +++ b/__tests__/date-fns-jalali.test.ts @@ -74,6 +74,18 @@ describe("DateFnsJalali", () => { ); }); + it("DateFnsJalali -- startOfYear", () => { + expect(utils.formatByString(utils.startOfYear(date), dateTimeFormat)).toBe( + "1397/01/01 00:00" + ); + }); + + it("DateFnsJalali -- endOfYear", () => { + expect(utils.formatByString(utils.endOfYear(date), dateTimeFormat)).toBe( + "1397/12/29 23:59" + ); + }); + it("DateFnsJalali -- startOfMonth", () => { expect(utils.formatByString(utils.startOfMonth(date), dateTimeFormat)).toBe( "1397/08/01 00:00" diff --git a/__tests__/hijri.test.ts b/__tests__/hijri.test.ts index 1057c71a..095a45ad 100644 --- a/__tests__/hijri.test.ts +++ b/__tests__/hijri.test.ts @@ -46,6 +46,22 @@ describe("Hijri", () => { expect(hijriiUtils.getWeekdays()).toEqual(["ح", "ن", "ث", "ر", "خ", "ج", "س"]); }); + it("Hijri -- endOfYear", () => { + const date = hijriiUtils.date(TEST_TIMESTAMP); + + expect(hijriiUtils.endOfYear(date).toISOString()).toEqual( + "2019-08-30T23:59:59.999Z" + ); + }); + + it("Hijri -- startOfYear", () => { + const date = hijriiUtils.date(TEST_TIMESTAMP); + + expect(hijriiUtils.startOfYear(date).toISOString()).toEqual( + "2018-09-11T00:00:00.000Z" + ); + }); + it("Hijri -- endOfMonth", () => { const date = hijriiUtils.date(TEST_TIMESTAMP); diff --git a/__tests__/jalaali.test.ts b/__tests__/jalaali.test.ts index 0ef94e00..b377b658 100644 --- a/__tests__/jalaali.test.ts +++ b/__tests__/jalaali.test.ts @@ -49,6 +49,22 @@ describe("Jalaali", () => { expect(jalaaliUtils.getWeekdays()).toEqual(["ش", "ی", "د", "س", "چ", "پ", "ج"]); }); + it("Jalaali -- endOfYear", () => { + const date = jalaaliUtils.date(TEST_TIMESTAMP); + + expect(jalaaliUtils.endOfYear(date).toISOString()).toEqual( + "2019-03-20T23:59:59.999Z" + ); + }); + + it("Jalaali -- startOfYear", () => { + const date = jalaaliUtils.date(TEST_TIMESTAMP); + + expect(jalaaliUtils.startOfYear(date).toISOString()).toEqual( + "2018-03-21T00:00:00.000Z" + ); + }); + it("Jalaali -- endOfMonth", () => { const date = jalaaliUtils.date(TEST_TIMESTAMP); diff --git a/__tests__/local-date-calculations.test.ts b/__tests__/local-date-calculations.test.ts index 1177cadb..2a364020 100644 --- a/__tests__/local-date-calculations.test.ts +++ b/__tests__/local-date-calculations.test.ts @@ -49,6 +49,18 @@ describe("DateTime calculations", () => { ); }); + localDateutilsTest("startOfYear", (date, utils, lib) => { + expect(utils.formatByString(utils.startOfYear(date), formats.dateTime[lib])).toBe( + "2018-01-01 00:00" + ); + }); + + localDateutilsTest("endOfYear", (date, utils, lib) => { + expect(utils.formatByString(utils.endOfYear(date), formats.dateTime[lib])).toBe( + "2018-12-31 23:59" + ); + }); + localDateutilsTest("startOfMonth", (date, utils, lib) => { expect(utils.formatByString(utils.startOfMonth(date), formats.dateTime[lib])).toBe( "2018-10-01 00:00" diff --git a/packages/core/IUtils.d.ts b/packages/core/IUtils.d.ts index 7c5c9907..23092b11 100644 --- a/packages/core/IUtils.d.ts +++ b/packages/core/IUtils.d.ts @@ -109,6 +109,8 @@ export interface IUtils { isWithinRange(value: TDate, range: [TDate, TDate]): boolean; + startOfYear(value: TDate): TDate; + endOfYear(value: TDate): TDate; startOfMonth(value: TDate): TDate; endOfMonth(value: TDate): TDate; startOfWeek(value: TDate): TDate; diff --git a/packages/core/dev-utils.js b/packages/core/dev-utils.js index 852ca179..e5882c7d 100644 --- a/packages/core/dev-utils.js +++ b/packages/core/dev-utils.js @@ -14,7 +14,7 @@ exports.createRollupConfig = (typescript) => { output: { file: `build/index.esm.js`, format: "esm", - exports: "auto" + exports: "auto", }, plugins: [nodeResolve({ extensions }), typescriptPlugin({ typescript })], }, @@ -24,7 +24,7 @@ exports.createRollupConfig = (typescript) => { output: { file: `build/index.js`, format: "cjs", - exports: "auto" + exports: "auto", }, plugins: [nodeResolve({ extensions }), typescriptPlugin({ typescript })], }, diff --git a/packages/date-fns-jalali/src/date-fns-jalali-utils.ts b/packages/date-fns-jalali/src/date-fns-jalali-utils.ts index 0abcccdf..11d36a89 100644 --- a/packages/date-fns-jalali/src/date-fns-jalali-utils.ts +++ b/packages/date-fns-jalali/src/date-fns-jalali-utils.ts @@ -248,6 +248,14 @@ export default class DateFnsJalaliUtils implements IUtils { return isSameHour(value, comparing); }; + public startOfYear = (value: Date) => { + return startOfYear(value); + }; + + public endOfYear = (value: Date) => { + return endOfYear(value); + }; + public startOfMonth = (value: Date) => { return startOfMonth(value); }; diff --git a/packages/date-fns/src/date-fns-utils.ts b/packages/date-fns/src/date-fns-utils.ts index 2555a2a0..e5509464 100644 --- a/packages/date-fns/src/date-fns-utils.ts +++ b/packages/date-fns/src/date-fns-utils.ts @@ -241,6 +241,14 @@ export default class DateFnsUtils implements IUtils { return isSameHour(value, comparing); }; + public startOfYear = (value: Date) => { + return startOfYear(value); + }; + + public endOfYear = (value: Date) => { + return endOfYear(value); + }; + public startOfMonth = (value: Date) => { return startOfMonth(value); }; diff --git a/packages/dayjs/src/dayjs-utils.ts b/packages/dayjs/src/dayjs-utils.ts index faaf607f..57788377 100644 --- a/packages/dayjs/src/dayjs-utils.ts +++ b/packages/dayjs/src/dayjs-utils.ts @@ -273,6 +273,14 @@ export default class DayjsUtils implements IUtils { + return date.startOf("year") as TDate; + }; + + public endOfYear = (date: Dayjs) => { + return date.endOf("year") as TDate; + }; + public startOfMonth = (date: Dayjs) => { return date.clone().startOf("month") as TDate; }; diff --git a/packages/hijri/src/hijri-utils.ts b/packages/hijri/src/hijri-utils.ts index 713c59ae..b209d8b2 100644 --- a/packages/hijri/src/hijri-utils.ts +++ b/packages/hijri/src/hijri-utils.ts @@ -104,6 +104,14 @@ export default class MomentUtils extends DefaultMomentUtils { return date.daysInMonth(); }; + public startOfYear = (date: Moment) => { + return date.clone().startOf("iYear"); + }; + + public endOfYear = (date: Moment) => { + return date.clone().endOf("iYear"); + }; + public startOfMonth = (date: Moment) => { return date.clone().startOf("iMonth"); }; diff --git a/packages/jalaali/src/jalaali-utils.ts b/packages/jalaali/src/jalaali-utils.ts index 2845e585..4de234f5 100644 --- a/packages/jalaali/src/jalaali-utils.ts +++ b/packages/jalaali/src/jalaali-utils.ts @@ -103,6 +103,14 @@ export default class MomentUtils extends DefaultMomentUtils { return date.daysInMonth(); }; + public startOfYear = (date: Moment) => { + return date.clone().startOf("jYear"); + }; + + public endOfYear = (date: Moment) => { + return date.clone().endOf("jYear"); + }; + public startOfMonth = (date: Moment) => { return date.clone().startOf("jMonth"); }; diff --git a/packages/js-joda/src/js-joda-utils.ts b/packages/js-joda/src/js-joda-utils.ts index e2044935..4d8c0bba 100644 --- a/packages/js-joda/src/js-joda-utils.ts +++ b/packages/js-joda/src/js-joda-utils.ts @@ -301,6 +301,14 @@ export default class JsJodaUtils implements IUtils { return datedate.isBefore(valuedate); } + startOfYear(date: Temporal) { + return Year.from(date).atMonth(1).atDay(1).atStartOfDay(); + } + + endOfYear(date: Temporal) { + return Year.from(date).atMonth(12).atEndOfMonth().atTime(LocalTime.MAX); + } + startOfMonth(date: Temporal) { return YearMonth.from(date).atDay(1).atStartOfDay(); } diff --git a/packages/js-joda/type/index.d.ts b/packages/js-joda/type/index.d.ts index a61dd58a..82765955 100644 --- a/packages/js-joda/type/index.d.ts +++ b/packages/js-joda/type/index.d.ts @@ -1,5 +1,5 @@ declare module "@date-io/type" { - import {Temporal} from "@js-joda/core"; + import { Temporal } from "@js-joda/core"; export type DateType = Temporal; } diff --git a/packages/luxon/src/luxon-utils.ts b/packages/luxon/src/luxon-utils.ts index f2089f1d..12f9b2e5 100644 --- a/packages/luxon/src/luxon-utils.ts +++ b/packages/luxon/src/luxon-utils.ts @@ -282,6 +282,14 @@ export default class LuxonUtils implements IUtils { }); }; + public startOfYear = (value: DateTime) => { + return value.startOf("year"); + }; + + public endOfYear = (value: DateTime) => { + return value.endOf("year"); + }; + public startOfMonth = (value: DateTime) => { return value.startOf("month"); }; diff --git a/packages/moment/src/moment-utils.ts b/packages/moment/src/moment-utils.ts index ae1d061b..8c13be58 100644 --- a/packages/moment/src/moment-utils.ts +++ b/packages/moment/src/moment-utils.ts @@ -267,6 +267,14 @@ export default class MomentUtils implements IUtils { return ampm === "am" ? "AM" : "PM"; // fallback for de, ru, ...etc }; + public startOfYear = (date: Moment) => { + return date.clone().startOf("year"); + }; + + public endOfYear = (date: Moment) => { + return date.clone().endOf("year"); + }; + public startOfMonth = (date: Moment) => { return date.clone().startOf("month"); };