Skip to content

Instantly share code, notes, and snippets.

@megasmack
Created January 22, 2026 20:26
Show Gist options
  • Select an option

  • Save megasmack/b90ac1a1d500a2b25cce6ce09fd3652b to your computer and use it in GitHub Desktop.

Select an option

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.
/**
* 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