Created
January 22, 2026 20:26
-
-
Save megasmack/b90ac1a1d500a2b25cce6ce09fd3652b to your computer and use it in GitHub Desktop.
Take a Date without time to parse without changing the date when parsed into a date object.
This file contains hidden or 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
| /** | |
| * Can be used to set the time and timezone offset for simple Date comparisons. | |
| * @param {string} dateValue - Date to be converted | |
| * @param {number} [offset] - Timezone offset | |
| * @returns {number} - Converted date in milliseconds. | |
| */ | |
| export const normalizeTime = (dateValue, offset = undefined) => { | |
| if (dateValue) { | |
| const thisDate = new Date(dateValue); | |
| let msDate = thisDate.getTime(); | |
| const dateValueOffset = offset || thisDate?.getTimezoneOffset(); | |
| msDate = msDate + dateValueOffset * 60 * 1000; // Add the offset in milliseconds | |
| return new Date(msDate)?.setHours(0, 0, 0, 0); | |
| } | |
| return undefined; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment