Skip to content

Commit

Permalink
imp: Now dates-related functions accepts both String & Date values.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byloth committed Aug 8, 2024
1 parent 84a71c5 commit f807a0d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@byloth/core",
"version": "1.5.2",
"version": "1.5.3",
"description": "An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧",
"keywords": [
"Core",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const VERSION = "1.5.2";
export const VERSION = "1.5.3";

export type { Constructor, Interval, Timeout } from "./core/types.js";

Expand Down
14 changes: 11 additions & 3 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ export enum DateUnit
Year = 365 * Day
}

export function dateDifference(start: Date, end: Date, unit = DateUnit.Day): number
export function dateDifference(start: string | Date, end: string | Date, unit = DateUnit.Day): number
{
start = new Date(start);
end = new Date(end);

return Math.floor((end.getTime() - start.getTime()) / unit);
}

export function dateRange(start: Date, end: Date, offset = DateUnit.Day): SmartIterator<Date>
export function dateRange(start: string | Date, end: string | Date, offset = DateUnit.Day): SmartIterator<Date>
{
start = new Date(start);
end = new Date(end);

return new SmartIterator<Date>(function* ()
{
const endTime = end.getTime();
Expand All @@ -33,7 +39,9 @@ export function dateRange(start: Date, end: Date, offset = DateUnit.Day): SmartI
});
}

export function dateRound(date: Date, unit = DateUnit.Day): Date
export function dateRound(date: string | Date, unit = DateUnit.Day): Date
{
date = new Date(date);

return new Date(Math.floor(date.getTime() / unit) * unit);
}

0 comments on commit f807a0d

Please sign in to comment.