This commit is contained in:
Adria Navarro 2025-03-06 11:37:13 +01:00
parent 06b11adb01
commit 2462b80991
2 changed files with 29 additions and 1 deletions

View File

@ -134,7 +134,7 @@ export const duration = (str: any, pattern: any, format?: any) => {
} }
} }
export const difference = (from: string, to: string, units: UnitType) => { export const difference = (from: string, to: string, units?: UnitType) => {
const result = dayjs(new Date(from)).diff(dayjs(new Date(to)), units) const result = dayjs(new Date(from)).diff(dayjs(new Date(to)), units)
return result return result
} }

View File

@ -0,0 +1,28 @@
import * as date from "../../src/helpers/date"
describe("date helper", () => {
describe("difference", () => {
it("should return the difference between two dates", () => {
const result = date.difference(
"2021-01-02T12:34:56.789",
"2021-01-01T01:00:00"
)
const expected =
1 * 24 * 60 * 60 * 1000 + // 1 day
11 * 60 * 60 * 1000 + // 11 hours
34 * 60 * 1000 + // 34 minutes
56 * 1000 + // seconds
789 // milliseconds
expect(result).toEqual(expected)
})
it("should be able to set the time unit", () => {
const result = date.difference(
"2021-01-02T12:34:56",
"2021-01-01T01:00:00",
"days"
)
expect(result).toEqual(1)
})
})
})