From 95dfd25242fc7c10b7d94d4f3ac86326e668cfbc Mon Sep 17 00:00:00 2001 From: WilcoSp Date: Wed, 18 Sep 2024 15:38:25 +0200 Subject: [PATCH 1/8] moving the date over flow handling to a separate function --- src/addMonth.ts | 30 +++++++++++++++--------------- src/addYear.ts | 31 +++++++++++++++++-------------- src/handleDateOverflow.ts | 23 +++++++++++++++++++++++ 3 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 src/handleDateOverflow.ts diff --git a/src/addMonth.ts b/src/addMonth.ts index 1d6d389..d07339e 100644 --- a/src/addMonth.ts +++ b/src/addMonth.ts @@ -1,6 +1,5 @@ -import { date } from "./date" -import { monthDays } from "./monthDays" -import type { DateInput, MaybeDateInput } from "./types" +import { handleOverflow } from "./handleDateOverflow" +import type { MaybeDateInput } from "./types" /** * Returns a new date object 1/n months after the original one. Keep in mind if you @@ -11,17 +10,18 @@ import type { DateInput, MaybeDateInput } from "./types" * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. */ export function addMonth(inputDate?: MaybeDateInput, count = 1, dateOverflow = false) { - const d = date(inputDate) - const dayOfMonth = d.getDate() - // If overflowing is disallowed, set the date back to the first of the month - if (!dateOverflow) d.setDate(1) - d.setMonth(d.getMonth() + count) + return handleOverflow(inputDate, dateOverflow, (d) => d.setMonth(d.getMonth() + count)) + // const d = date(inputDate) + // const dayOfMonth = d.getDate() + // // If overflowing is disallowed, set the date back to the first of the month + // if (!dateOverflow) d.setDate(1) + // d.setMonth(d.getMonth() + count) - // If overflowing is disallowed, we need to set the date back to the proper - // day or the last day of the month. - if (!dateOverflow) { - const daysInMonth = monthDays(d) - d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth) - } - return d + // // If overflowing is disallowed, we need to set the date back to the proper + // // day or the last day of the month. + // if (!dateOverflow) { + // const daysInMonth = monthDays(d) + // d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth) + // } + // return d } diff --git a/src/addYear.ts b/src/addYear.ts index 93a04c4..5859b6c 100644 --- a/src/addYear.ts +++ b/src/addYear.ts @@ -1,5 +1,4 @@ -import { date } from "./date" -import { monthDays } from "./monthDays" +import { handleOverflow } from "./handleDateOverflow" import type { MaybeDateInput } from "./types" /** @@ -11,18 +10,22 @@ import type { MaybeDateInput } from "./types" * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. */ export function addYear(inputDate?: MaybeDateInput, count = 1, dateOverflow = false) { - const d = date(inputDate) - const dayOfMonth = d.getDate() - // If overflowing is disallowed, set the date back to the first of the month - if (!dateOverflow) d.setDate(1) + // const d = date(inputDate) + // const dayOfMonth = d.getDate() + // // If overflowing is disallowed, set the date back to the first of the month + // if (!dateOverflow) d.setDate(1) - d.setFullYear(d.getFullYear() + count) + // d.setFullYear(d.getFullYear() + count) - // If overflowing is disallowed, we need to set the date back to the proper - // day or the last day of the month. - if (!dateOverflow) { - const daysInMonth = monthDays(d) - d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth) - } - return d + // // If overflowing is disallowed, we need to set the date back to the proper + // // day or the last day of the month. + // if (!dateOverflow) { + // const daysInMonth = monthDays(d) + // d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth) + // } + // return d + + return handleOverflow(inputDate, dateOverflow, (d) => + d.setFullYear(d.getFullYear() + count) + ) } diff --git a/src/handleDateOverflow.ts b/src/handleDateOverflow.ts new file mode 100644 index 0000000..337e950 --- /dev/null +++ b/src/handleDateOverflow.ts @@ -0,0 +1,23 @@ +import { date } from "./date" +import { monthDays } from "./monthDays" +import { MaybeDateInput } from "./types" + +export function handleOverflow( + inputDate: MaybeDateInput | undefined, + allowOverflow = false, + action: (d: Date) => void +) { + let d = date(inputDate) + const dayOfMonth = d.getDate() + // If overflowing is disallowed, set the date back to the first of the month + if (!allowOverflow) d.setDate(1) + // d.setMonth(d.getMonth() + count) + action(d) + // If overflowing is disallowed, we need to set the date back to the proper + // day or the last day of the month. + if (!allowOverflow) { + const daysInMonth = monthDays(d) + d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth) + } + return d +} From f11251f68a57357cc591079d34a48aadb3d2b6d8 Mon Sep 17 00:00:00 2001 From: WilcoSp Date: Sat, 12 Oct 2024 10:07:05 +0200 Subject: [PATCH 2/8] refactored the handleOverflow to have dateOverflow last removed old code at addMonth/year created setMonth/year and with their tests --- src/__tests__/setMonth.spec.ts | 35 ++++++++++++++++++++++++++++++++++ src/__tests__/setYear.spec.ts | 19 ++++++++++++++++++ src/addMonth.ts | 15 +-------------- src/addYear.ts | 21 ++++---------------- src/handleDateOverflow.ts | 9 +++++---- src/setMonth.ts | 14 ++++++++++++++ src/setYear.ts | 17 +++++++++++++++++ 7 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 src/__tests__/setMonth.spec.ts create mode 100644 src/__tests__/setYear.spec.ts create mode 100644 src/setMonth.ts create mode 100644 src/setYear.ts diff --git a/src/__tests__/setMonth.spec.ts b/src/__tests__/setMonth.spec.ts new file mode 100644 index 0000000..999c32d --- /dev/null +++ b/src/__tests__/setMonth.spec.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from "vitest" +import { setMonth } from "../setMonth" +import { date } from "../date" + +describe("setMonth", () => { + it("should set month to Feb", () => { + expect(setMonth("2019-10-10", 1)).toEqual(date("2019-02-10")) + }) + + it("should prevent overflow in leap year", () => { + expect(setMonth("2024-03-31", 1)).toEqual(date("2024-02-29")) + }) + + it("should prevent overflow in none leap year", () => { + expect(setMonth("2023-03-31", 1)).toEqual(date("2023-02-28")) + }) + + it("should overflow when month has less days", () => { + expect(setMonth("2023-03-31", 3, true)).toEqual(date("2023-05-01")) + }) + + it("should overflow in months when given more than 11", () => { + expect(setMonth("2020-01-05", 13)).toEqual(date("2021-02-05")) + }) + + it("should undeflow in months when giving negative", () => { + expect(setMonth("2020-01-05", -3)).toEqual(date("2019-10-05")) + }) + + it("should set month to July with current time", () => { + const d = date() + d.setMonth(7) + expect(setMonth(null, 7)).toEqual(d) + }) +}) diff --git a/src/__tests__/setYear.spec.ts b/src/__tests__/setYear.spec.ts new file mode 100644 index 0000000..d0001af --- /dev/null +++ b/src/__tests__/setYear.spec.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from "vitest" +import { setYear } from "../setYear" +import { date } from "../date" + +describe("setYear", () => { + it("should set year to 1998", () => { + expect(setYear("2010-06-29", 1998)).toEqual(date("1998-06-29")) + }) + + it("should set year to 2017 with current time", () => { + const d = date() + d.setFullYear(2017) + expect(setYear(null, 2017)).toEqual(d) + }) + + it("should handle overflow from leap year to not leap year", () => { + expect(setYear("2024-02-29", 2023)).toEqual(date("2023-02-28")) + }) +}) diff --git a/src/addMonth.ts b/src/addMonth.ts index d07339e..385b359 100644 --- a/src/addMonth.ts +++ b/src/addMonth.ts @@ -10,18 +10,5 @@ import type { MaybeDateInput } from "./types" * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. */ export function addMonth(inputDate?: MaybeDateInput, count = 1, dateOverflow = false) { - return handleOverflow(inputDate, dateOverflow, (d) => d.setMonth(d.getMonth() + count)) - // const d = date(inputDate) - // const dayOfMonth = d.getDate() - // // If overflowing is disallowed, set the date back to the first of the month - // if (!dateOverflow) d.setDate(1) - // d.setMonth(d.getMonth() + count) - - // // If overflowing is disallowed, we need to set the date back to the proper - // // day or the last day of the month. - // if (!dateOverflow) { - // const daysInMonth = monthDays(d) - // d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth) - // } - // return d + return handleOverflow(inputDate, (d) => d.setMonth(d.getMonth() + count), dateOverflow) } diff --git a/src/addYear.ts b/src/addYear.ts index 5859b6c..eaeee57 100644 --- a/src/addYear.ts +++ b/src/addYear.ts @@ -10,22 +10,9 @@ import type { MaybeDateInput } from "./types" * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. */ export function addYear(inputDate?: MaybeDateInput, count = 1, dateOverflow = false) { - // const d = date(inputDate) - // const dayOfMonth = d.getDate() - // // If overflowing is disallowed, set the date back to the first of the month - // if (!dateOverflow) d.setDate(1) - - // d.setFullYear(d.getFullYear() + count) - - // // If overflowing is disallowed, we need to set the date back to the proper - // // day or the last day of the month. - // if (!dateOverflow) { - // const daysInMonth = monthDays(d) - // d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth) - // } - // return d - - return handleOverflow(inputDate, dateOverflow, (d) => - d.setFullYear(d.getFullYear() + count) + return handleOverflow( + inputDate, + (d) => d.setFullYear(d.getFullYear() + count), + dateOverflow ) } diff --git a/src/handleDateOverflow.ts b/src/handleDateOverflow.ts index 337e950..b281b11 100644 --- a/src/handleDateOverflow.ts +++ b/src/handleDateOverflow.ts @@ -4,18 +4,19 @@ import { MaybeDateInput } from "./types" export function handleOverflow( inputDate: MaybeDateInput | undefined, - allowOverflow = false, - action: (d: Date) => void + action: (d: Date) => void, + dateOverflow = false ) { let d = date(inputDate) const dayOfMonth = d.getDate() // If overflowing is disallowed, set the date back to the first of the month - if (!allowOverflow) d.setDate(1) + if (!dateOverflow) d.setDate(1) // d.setMonth(d.getMonth() + count) + action(d) // If overflowing is disallowed, we need to set the date back to the proper // day or the last day of the month. - if (!allowOverflow) { + if (!dateOverflow) { const daysInMonth = monthDays(d) d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth) } diff --git a/src/setMonth.ts b/src/setMonth.ts new file mode 100644 index 0000000..dd5bc7a --- /dev/null +++ b/src/setMonth.ts @@ -0,0 +1,14 @@ +import { MaybeDateInput } from "./types" +import { date } from "./date" +import { monthDays } from "./monthDays" +import { handleOverflow } from "./handleDateOverflow" + +/** + * set the year of a date object with optional month and day para + * @param inputDate - a date or null for current time + * @param month - the month you want the date set to (months are 1-12/jan-dec) (can over/underflow) + * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. + */ +export function setMonth(inputDate: MaybeDateInput, month: number, dateOverflow = false) { + return handleOverflow(inputDate, (d) => d.setMonth(month), dateOverflow) +} diff --git a/src/setYear.ts b/src/setYear.ts new file mode 100644 index 0000000..1ccf1cf --- /dev/null +++ b/src/setYear.ts @@ -0,0 +1,17 @@ +import { date } from "./date" +import { MaybeDateInput } from "./types" +import { handleOverflow } from "./handleDateOverflow" + +/** + * set the year of a date object with optional month and day para + * @param inputDate - a date or null for current time + * @param year - the full year you want the date to be set to + + */ +export function setYear( + inputDate: MaybeDateInput, + year: number, + dateOverflow = false +): Date { + return handleOverflow(inputDate, (d) => d.setFullYear(year), dateOverflow) +} From f682e63b8f55aa7de07377cb275462875949649d Mon Sep 17 00:00:00 2001 From: WilcoSp Date: Sat, 12 Oct 2024 11:08:15 +0200 Subject: [PATCH 3/8] added setMilliseconds/Seconds/Minutes/Hour and their tests --- src/__tests__/setHour.spec.ts | 23 +++++++++++++++++++++ src/__tests__/setMilliseconds.spec.ts | 29 +++++++++++++++++++++++++++ src/__tests__/setMinutes.spec.ts | 23 +++++++++++++++++++++ src/__tests__/setSeconds.spec.ts | 23 +++++++++++++++++++++ src/setHour.ts | 13 ++++++++++++ src/setMilliseconds.ts | 13 ++++++++++++ src/setMinutes.ts | 13 ++++++++++++ src/setMonth.ts | 8 +++----- src/setSeconds.ts | 13 ++++++++++++ src/setYear.ts | 4 ++-- 10 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 src/__tests__/setHour.spec.ts create mode 100644 src/__tests__/setMilliseconds.spec.ts create mode 100644 src/__tests__/setMinutes.spec.ts create mode 100644 src/__tests__/setSeconds.spec.ts create mode 100644 src/setHour.ts create mode 100644 src/setMilliseconds.ts create mode 100644 src/setMinutes.ts create mode 100644 src/setSeconds.ts diff --git a/src/__tests__/setHour.spec.ts b/src/__tests__/setHour.spec.ts new file mode 100644 index 0000000..4c553a7 --- /dev/null +++ b/src/__tests__/setHour.spec.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from "vitest" +import { setHour } from "../setHour" +import { date } from "../date" + +describe("setHour", () => { + it("should set the minute", () => { + expect(setHour("2024-05-06 10:37", 17)).toEqual(date("2024-05-06 17:37")) + }) + + it("should overflow when given more than 12 hours", () => { + expect(setHour("2024-07-28 16:17", 37)).toEqual(date("2024-07-29 13:17")) + }) + + it("should underflow when given a negative number", () => { + expect(setHour("2024-03-20 13:06", -6)).toEqual(date("2024-03-19 18:06")) + }) + + it("should set hour to 6am from current time", () => { + const d = date() + d.setHours(6) + expect(setHour(null, 6)).toEqual(d) + }) +}) diff --git a/src/__tests__/setMilliseconds.spec.ts b/src/__tests__/setMilliseconds.spec.ts new file mode 100644 index 0000000..697caef --- /dev/null +++ b/src/__tests__/setMilliseconds.spec.ts @@ -0,0 +1,29 @@ +import { describe, expect, it } from "vitest" +import { setMilliseconds } from "../setMilliseconds" +import { date } from "../date" + +describe("setSeconds", () => { + it("should set the minute", () => { + expect(setMilliseconds("2024-05-06 10:00:00", 445)).toEqual( + date("2024-05-06 10:00:00.445") + ) + }) + + it("should overflow when given more than 59 seconds", () => { + expect(setMilliseconds("2024-07-28 16:10", 1584)).toEqual( + date("2024-07-28 16:10:01.584") + ) + }) + + it("should underflow when given a negative number", () => { + expect(setMilliseconds("2024-03-20 13:00", -40)).toEqual( + date("2024-03-20 12:59:59.960") + ) + }) + + it("should set ms to 54 from current time", () => { + const d = date() + d.setMilliseconds(54) + expect(setMilliseconds(null, 54)).toEqual(d) + }) +}) diff --git a/src/__tests__/setMinutes.spec.ts b/src/__tests__/setMinutes.spec.ts new file mode 100644 index 0000000..52ec36e --- /dev/null +++ b/src/__tests__/setMinutes.spec.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from "vitest" +import { setMinutes } from "../setMinutes" +import { date } from "../date" + +describe("setMinutes", () => { + it("should set the minute", () => { + expect(setMinutes("2024-05-06 10:00", 45)).toEqual(date("2024-05-06 10:45")) + }) + + it("should overflow when given more than 59 minutes", () => { + expect(setMinutes("2024-07-28 16:10", 84)).toEqual(date("2024-07-28 17:24")) + }) + + it("should underflow when given a negative number", () => { + expect(setMinutes("2024-03-20 13:00", -40)).toEqual(date("2024-03-20 12:20")) + }) + + it("should set minutes to 30 from current time", () => { + const d = date() + d.setMinutes(30) + expect(setMinutes(null, 30)).toEqual(d) + }) +}) diff --git a/src/__tests__/setSeconds.spec.ts b/src/__tests__/setSeconds.spec.ts new file mode 100644 index 0000000..263ab02 --- /dev/null +++ b/src/__tests__/setSeconds.spec.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from "vitest" +import { setSeconds } from "../setSeconds" +import { date } from "../date" + +describe("setSeconds", () => { + it("should set the minute", () => { + expect(setSeconds("2024-05-06 10:00:00", 45)).toEqual(date("2024-05-06 10:00:45")) + }) + + it("should overflow when given more than 59 seconds", () => { + expect(setSeconds("2024-07-28 16:10", 84)).toEqual(date("2024-07-28 16:11:24")) + }) + + it("should underflow when given a negative number", () => { + expect(setSeconds("2024-03-20 13:00", -40)).toEqual(date("2024-03-20 12:59:20")) + }) + + it("should set seconds to 54 from current time", () => { + const d = date() + d.setSeconds(54) + expect(setSeconds(null, 54)).toEqual(d) + }) +}) diff --git a/src/setHour.ts b/src/setHour.ts new file mode 100644 index 0000000..38a27f6 --- /dev/null +++ b/src/setHour.ts @@ -0,0 +1,13 @@ +import { date } from "./date" +import { MaybeDateInput } from "./types" + +/** + * set the hour of a date object + * @param [inputDate] - a date or null for current time + * @param hour - the hour you want the date set to (0 - 23) (can over/underflow) + */ +export function setHour(inputDate: MaybeDateInput, hour: number) { + const d = date(inputDate) + d.setHours(hour) + return d +} diff --git a/src/setMilliseconds.ts b/src/setMilliseconds.ts new file mode 100644 index 0000000..4db4935 --- /dev/null +++ b/src/setMilliseconds.ts @@ -0,0 +1,13 @@ +import { date } from "./date" +import { MaybeDateInput } from "./types" + +/** + * set the millisecond of a date object + * @param [inputDate] - a date or null for current time + * @param second - the milliseconds you want the date set to (0 - 999) (can over/underflow) + */ +export function setMilliseconds(inputDate: MaybeDateInput, ms: number) { + const d = date(inputDate) + d.setMilliseconds(ms) + return d +} diff --git a/src/setMinutes.ts b/src/setMinutes.ts new file mode 100644 index 0000000..d2c4855 --- /dev/null +++ b/src/setMinutes.ts @@ -0,0 +1,13 @@ +import { date } from "./date" +import { MaybeDateInput } from "./types" + +/** + * set the minute of a date object + * @param [inputDate] - a date or null for current time + * @param minute - the minute you want the date set to (0 - 59) (can over/underflow) + */ +export function setMinutes(inputDate: MaybeDateInput, minute: number) { + const d = date(inputDate) + d.setMinutes(minute) + return d +} diff --git a/src/setMonth.ts b/src/setMonth.ts index dd5bc7a..6080258 100644 --- a/src/setMonth.ts +++ b/src/setMonth.ts @@ -1,11 +1,9 @@ -import { MaybeDateInput } from "./types" -import { date } from "./date" -import { monthDays } from "./monthDays" import { handleOverflow } from "./handleDateOverflow" +import { MaybeDateInput } from "./types" /** - * set the year of a date object with optional month and day para - * @param inputDate - a date or null for current time + * set the month of a date + * @param [inputDate] - a date or null for current time * @param month - the month you want the date set to (months are 1-12/jan-dec) (can over/underflow) * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. */ diff --git a/src/setSeconds.ts b/src/setSeconds.ts new file mode 100644 index 0000000..3e1e772 --- /dev/null +++ b/src/setSeconds.ts @@ -0,0 +1,13 @@ +import { date } from "./date" +import { MaybeDateInput } from "./types" + +/** + * set the second of a date object + * @param [inputDate] - a date or null for current time + * @param second - the second you want the date set to (0 - 59) (can over/underflow) + */ +export function setSeconds(inputDate: MaybeDateInput, second: number) { + const d = date(inputDate) + d.setSeconds(second) + return d +} diff --git a/src/setYear.ts b/src/setYear.ts index 1ccf1cf..45bf4e8 100644 --- a/src/setYear.ts +++ b/src/setYear.ts @@ -3,10 +3,10 @@ import { MaybeDateInput } from "./types" import { handleOverflow } from "./handleDateOverflow" /** - * set the year of a date object with optional month and day para + * set the year of a date object * @param inputDate - a date or null for current time * @param year - the full year you want the date to be set to - + * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. */ export function setYear( inputDate: MaybeDateInput, From ef59448fc4ffafcafb1b2638e5a33ce7b86a01c2 Mon Sep 17 00:00:00 2001 From: WilcoSp Date: Sat, 12 Oct 2024 11:36:14 +0200 Subject: [PATCH 4/8] added setDayOfMonth and it's tests --- src/__tests__/setDayOfMonth.spec.ts | 35 +++++++++++++++++++++++++++++ src/setDayOfMonth.ts | 23 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/__tests__/setDayOfMonth.spec.ts create mode 100644 src/setDayOfMonth.ts diff --git a/src/__tests__/setDayOfMonth.spec.ts b/src/__tests__/setDayOfMonth.spec.ts new file mode 100644 index 0000000..bb42883 --- /dev/null +++ b/src/__tests__/setDayOfMonth.spec.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from "vitest" +import { setDayOfMonth } from "../setDayOfMonth" +import { date } from "../date" + +describe("setMinutes", () => { + it("should set the minute", () => { + expect(setDayOfMonth("2024-05-06", 20)).toEqual(date("2024-05-20")) + }) + + it("shouldn't overflow to the next month", () => { + expect(setDayOfMonth("2024-06-04", 31)).toEqual(date("2024-06-30")) + }) + + it("shouldn't overflow to the next month with Feb", () => { + expect(setDayOfMonth("2023-02-20", 31)).toEqual(date("2023-02-28")) + }) + + it("shouldn't overflow to the next month with Feb leap", () => { + expect(setDayOfMonth("2024-02-20", 31)).toEqual(date("2024-02-29")) + }) + + it("should overflow when allowing overflow", () => { + expect(setDayOfMonth("2024-07-28", 35, true)).toEqual(date("2024-08-04")) + }) + + it("should underflow when given a negative number", () => { + expect(setDayOfMonth("2024-07-20 ", -3)).toEqual(date("2024-06-27")) + }) + + it("should set day to the 3th from current month", () => { + const d = date() + d.setDate(3) + expect(setDayOfMonth(null, 3)).toEqual(d) + }) +}) diff --git a/src/setDayOfMonth.ts b/src/setDayOfMonth.ts new file mode 100644 index 0000000..06a02e3 --- /dev/null +++ b/src/setDayOfMonth.ts @@ -0,0 +1,23 @@ +import { date } from "./date" +import { monthDays } from "./monthDays" +import { MaybeDateInput } from "./types" + +/** + * set the day of the month in a date object + * @param [inputDate] - a date or null for current time + * @param day - the day of the month you want the date set to (1-28/29/30/31) (can over/underflow) + * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the given day isn't in the current month + */ +export function setDayOfMonth( + inputDate: MaybeDateInput, + day: number, + dateOverflow = false +) { + const d = date(inputDate) + const daysInMonth = monthDays(d) + if (!dateOverflow) { + day = day > daysInMonth ? daysInMonth : day + } + d.setDate(day) + return d +} From 370c674ae6ba8146584d6b5288eee17683ec5e69 Mon Sep 17 00:00:00 2001 From: WilcoSp Date: Sat, 12 Oct 2024 11:49:37 +0200 Subject: [PATCH 5/8] adding exports for set* functions --- src/index.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/index.ts b/src/index.ts index 3854023..2c303a1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,6 +34,13 @@ export { sameSecond } from "./sameSecond" export { sameMinute } from "./sameMinute" export { sameHour } from "./sameHour" export { sameYear } from "./sameYear" +export { setMilliseconds } from "./setMilliseconds" +export { setSeconds } from "./setSeconds" +export { setMinutes } from "./setMinutes" +export { setHour } from "./setHour" +export { setDayOfMonth } from "./setDayOfMonth" +export { setMonth } from "./setMonth" +export { setYear } from "./setYear" export { weekEnd } from "./weekEnd" export { weekStart } from "./weekStart" export { yearDays } from "./yearDays" From f7f372ef133aa122c9d86b1c05f92ae0db5c43d2 Mon Sep 17 00:00:00 2001 From: WilcoSp Date: Sat, 12 Oct 2024 11:56:51 +0200 Subject: [PATCH 6/8] exporting handleOverflow --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 2c303a1..7dec127 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ export { dayStart } from "./dayStart" export { format } from "./format" export { formatStr } from "./formatStr" export { fourDigitYear } from "./fourDigitYear" +export { handleOverflow } from "./handleDateOverflow" export { hourEnd } from "./hourEnd" export { hourStart } from "./hourStart" export { iso8601 } from "./iso8601" From 7c899f55d34d41c64f114a2c66f982367aac8b0c Mon Sep 17 00:00:00 2001 From: WilcoSp Date: Sat, 12 Oct 2024 12:37:03 +0200 Subject: [PATCH 7/8] adding jsdoc for handleOverflow --- src/handleDateOverflow.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/handleDateOverflow.ts b/src/handleDateOverflow.ts index b281b11..2540790 100644 --- a/src/handleDateOverflow.ts +++ b/src/handleDateOverflow.ts @@ -2,6 +2,13 @@ import { date } from "./date" import { monthDays } from "./monthDays" import { MaybeDateInput } from "./types" +/** + * handles date overflow when changing month or year + * @param [inputDate] - A string, Date object or null for the current time + * @param action - the action that changes the month/year + * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. + * @returns a new Date object with the change year/month without overflowing (except if wanted to overflow) + */ export function handleOverflow( inputDate: MaybeDateInput | undefined, action: (d: Date) => void, From 77e8c58d4d13011a34f31815af881deec8ab5c7b Mon Sep 17 00:00:00 2001 From: WilcoSp Date: Sat, 12 Oct 2024 12:42:18 +0200 Subject: [PATCH 8/8] .. --- src/handleDateOverflow.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/handleDateOverflow.ts b/src/handleDateOverflow.ts index 2540790..e3d4c64 100644 --- a/src/handleDateOverflow.ts +++ b/src/handleDateOverflow.ts @@ -18,7 +18,6 @@ export function handleOverflow( const dayOfMonth = d.getDate() // If overflowing is disallowed, set the date back to the first of the month if (!dateOverflow) d.setDate(1) - // d.setMonth(d.getMonth() + count) action(d) // If overflowing is disallowed, we need to set the date back to the proper