Skip to content

Commit

Permalink
[3.0.0] Add daysFrom and daysBetween helpers
Browse files Browse the repository at this point in the history
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
ericbf committed May 20, 2023
1 parent 87df6c4 commit cca2c49
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
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)\"",
Expand Down
12 changes: 12 additions & 0 deletions src/daysBetween.test.ts
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)
})
})
8 changes: 8 additions & 0 deletions src/daysBetween.ts
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
12 changes: 12 additions & 0 deletions src/daysFrom.test.ts
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)
})
})
24 changes: 24 additions & 0 deletions src/daysFrom.ts
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

0 comments on commit cca2c49

Please sign in to comment.