-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3.0.0] Add
daysFrom
and daysBetween
helpers
We added these helpers that can calculate the number of days between two dates, either taking into account which comes first or not.
- Loading branch information
Showing
6 changed files
with
61 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
{ | ||
"name": "fast-ts-helpers", | ||
"version": "2.3.0", | ||
"version": "3.0.0", | ||
"private": true, | ||
"description": "A package containing some helpful functions, constants, types, and react hooks.", | ||
"license": "MIT", | ||
"repository": "github:ericmakesapps/fast-ts-helpers", | ||
"author": "Eric Ferreira <[email protected]>", | ||
"scripts": { | ||
"build": "tsc", | ||
"check": "run-p lint format \"test -- --watchAll=false\"", | ||
"fix": "run-s fix:lint fix:format \"test -- --watchAll=false\"", | ||
"check": "run-p lint format \"test -- --watch=false\"", | ||
"fix": "run-s fix:lint fix:format \"test -- --watch=false\"", | ||
"fix:format": "npm run format -- --write", | ||
"fix:lint": "npm run lint -- --fix", | ||
"format": "prettier -c --ignore-path .gitignore \"**/?*.@(js|ts|jsx|tsx|json|md|html|css|scss)\"", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import daysBetween from "./daysBetween" | ||
import parseDate from "./parseDate" | ||
|
||
describe("keys helper", () => { | ||
test("should return the positive number of days if `date` is before `toDate`", () => { | ||
expect(daysBetween(parseDate("2023-01-01"), parseDate("2023-01-15"))).toEqual(14) | ||
}) | ||
|
||
test("should return the positive number of days if `date` is after `toDate`", () => { | ||
expect(daysBetween(parseDate("2023-01-11"), parseDate("2023-01-7"))).toEqual(4) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import daysFrom from "./daysFrom" | ||
|
||
/** Get the number of days between the two passed dates, using UTC for each date (not counting the times of the dates). */ | ||
function daysBetween(date: Date, andDate: Date) { | ||
return Math.abs(daysFrom(date, andDate)) | ||
} | ||
|
||
export default daysBetween |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import daysFrom from "./daysFrom" | ||
import parseDate from "./parseDate" | ||
|
||
describe("keys helper", () => { | ||
test("should return the positive number of days if `date` is before `toDate`", () => { | ||
expect(daysFrom(parseDate("2023-01-01"), parseDate("2023-01-15"))).toEqual(14) | ||
}) | ||
|
||
test("should return the negative number of days if `date` is after `toDate`", () => { | ||
expect(daysFrom(parseDate("2023-01-11"), parseDate("2023-01-7"))).toEqual(-4) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** Get the number of days from one date to the next date, using UTC for each (not counting the times of the dates). If `date` is after `toDate`, the returned value will be negative. */ | ||
function daysFrom(date: Date, toDate: Date) { | ||
const d = new Date( | ||
date.getUTCFullYear(), | ||
date.getUTCMonth(), | ||
date.getUTCDate(), | ||
0, | ||
0, | ||
0 | ||
) | ||
|
||
const d2 = new Date( | ||
toDate.getUTCFullYear(), | ||
toDate.getUTCMonth(), | ||
toDate.getUTCDate(), | ||
0, | ||
0, | ||
0 | ||
) | ||
|
||
return Math.round((d2.getTime() - d.getTime()) / (1000 * 60 * 60 * 24)) | ||
} | ||
|
||
export default daysFrom |