Created
August 7, 2021 15:18
-
-
Save jsjoeio/a523e86a5eb49c31dae789e3fbdf8545 to your computer and use it in GitHub Desktop.
adjustDate
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
import { | |
subHours, | |
subMinutes, | |
} from "date-fns" | |
export const DEFAULT_CUSTOM_DAY_START = "00:00:00" | |
declare const _dateAdjusted: unique symbol | |
export type DateAdjustedToCustomDayStart = Date & { | |
readonly [_dateAdjusted]: Date | |
} | |
/** | |
* Helper function to adjust the date based on the customDayStart. | |
*/ | |
export function adjustDate( | |
date: Date, | |
customDayStart: string | null | |
): DateAdjustedToCustomDayStart { | |
if (!customDayStart || customDayStart === DEFAULT_CUSTOM_DAY_START) { | |
return date as DateAdjustedToCustomDayStart | |
} | |
// Split up the customDayStart into hours/minutes | |
// so we can subtract them | |
const [hours, minutes] = customDayStart | |
.split(":", 3) | |
.map((n) => parseInt(n, 10)) | |
return subHours( | |
subMinutes(date, minutes), | |
hours | |
) as DateAdjustedToCustomDayStart | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment