Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ jobs:
- run: yarn lint
- run: yarn build
- run: yarn jest
env:
TZ: Europe/Zurich

- name: Code Coverage Summary Report
uses: irongut/CodeCoverageSummary@v1.3.0
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `getDateWithoutTimeZone` utility function
- `getDateAddTimeZone` utility function

## [2.4.0] - 2026-04-29

### Added
Expand Down
4 changes: 4 additions & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ export default [
jest: true,
jsdocRequireJsdoc: true,
}),
{
files: ["src/lib/*.spec.ts"],
rules: { "max-lines": ["off"] },
},
];
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"prettier-check": "prettier --check .",
"prettier-write": "prettier --write .",
"start": "rollup -c -w",
"test": "yarn jest",
"test": "cross-env TZ=Europe/Zurich yarn jest",
"start-all": "concurrently \"yarn start\" \"yarn start-yalc\"",
"start-yalc": "yarn nodemon --watch dist -x \"yarn yalc push\""
},
Expand All @@ -53,6 +53,7 @@
"@types/rollup-plugin-peer-deps-external": "^2.2.5",
"@types/uuid": "^9.0.7",
"concurrently": "^8.0.1",
"cross-env": "^7.0.3",
"eslint": "^9.24.0",
"jest": "^29.6.1",
"jest-localstorage-mock": "^2.4.26",
Expand Down
30 changes: 30 additions & 0 deletions src/lib/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
getLastDayOfYear,
addQuarters,
getQuarter,
getDateAddTimeZone,
getDateWithoutTimeZone,
} from "./date";

describe("date tests", () => {
Expand Down Expand Up @@ -193,4 +195,32 @@ describe("date tests", () => {
])("addQuarters", (date, amount, expected) => {
expect(addQuarters(date, amount).getTime()).toBe(expected.getTime());
});

test.each([
[null as unknown as Date, new Date(Number.NaN)],
[undefined as unknown as Date, new Date(Number.NaN)],
[42 as unknown as Date, new Date(Number.NaN)],
["2015-03-15" as unknown as Date, new Date(Number.NaN)],
[{ year: 2014, month: 2, day: 15 } as unknown as Date, new Date(Number.NaN)],
[new Date(2026, 6, 5, 0, 0, 0, 0), new Date(2026, 6, 5, 0, 0, 0, 0 + 120 * 60_000)],
[new Date(2026, 6, 5, 1, 12, 13, 1), new Date(2026, 6, 5, 1, 12, 13, 1 + 120 * 60_000)],
[new Date(2026, 12, 5, 0, 0, 0, 0), new Date(2026, 12, 5, 0, 0, 0, 0 + 60 * 60_000)],
[new Date(2026, 12, 5, 1, 12, 13, 1), new Date(2026, 12, 5, 1, 12, 13, 1 + 60 * 60_000)],
])("getDateWithoutTimeZone", (date, expected) => {
expect(getDateWithoutTimeZone(date).getTime()).toBe(expected.getTime());
});

test.each([
[null as unknown as Date, new Date(Number.NaN)],
[undefined as unknown as Date, new Date(Number.NaN)],
[42 as unknown as Date, new Date(Number.NaN)],
["2015-03-15" as unknown as Date, new Date(Number.NaN)],
[{ year: 2014, month: 2, day: 15 } as unknown as Date, new Date(Number.NaN)],
[new Date(2026, 6, 5, 0, 0, 0, 0), new Date(2026, 6, 5, 0, 0, 0, 0 - 120 * 60_000)],
[new Date(2026, 6, 5, 1, 12, 13, 1), new Date(2026, 6, 5, 1, 12, 13, 1 - 120 * 60_000)],
[new Date(2026, 12, 5, 0, 0, 0, 0), new Date(2026, 12, 5, 0, 0, 0, 0 - 60 * 60_000)],
[new Date(2026, 12, 5, 1, 12, 13, 1), new Date(2026, 12, 5, 1, 12, 13, 1 - 60 * 60_000)],
])("getDateAddTimeZone", (date, expected) => {
expect(getDateAddTimeZone(date).getTime()).toBe(expected.getTime());
});
});
18 changes: 18 additions & 0 deletions src/lib/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,21 @@ export function getQuarter(date: Date): number {
export function addQuarters(date: Date, amount: number): Date {
return dateIsValid(date) ? addQuartersInternal(date, amount) : new Date(Number.NaN);
}

/**
* Get the date removing the time-zone offset
* @param date The date
* @returns the given date removing the time-zone offset
*/
export function getDateWithoutTimeZone(date: Date): Date {
return dateIsValid(date) ? new Date(date.getTime() - date.getTimezoneOffset() * 60_000) : new Date(Number.NaN);
}

/**
* Get the date adding the time-zone offset
* @param date The date
* @returns the given date adding the time-zone offset
*/
export function getDateAddTimeZone(date: Date): Date {
return dateIsValid(date) ? new Date(date.getTime() + date.getTimezoneOffset() * 60_000) : new Date(Number.NaN);
}
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2401,7 +2401,14 @@ create-require@^1.1.0:
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

cross-spawn@^7.0.3, cross-spawn@^7.0.6:
cross-env@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"
integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==
dependencies:
cross-spawn "^7.0.1"

cross-spawn@^7.0.1, cross-spawn@^7.0.3, cross-spawn@^7.0.6:
version "7.0.6"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.6.tgz#8a58fe78f00dcd70c370451759dfbfaf03e8ee9f"
integrity sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==
Expand Down
Loading