Skip to content

Instantly share code, notes, and snippets.

@jsjoeio
Created August 7, 2021 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsjoeio/a523e86a5eb49c31dae789e3fbdf8545 to your computer and use it in GitHub Desktop.
Save jsjoeio/a523e86a5eb49c31dae789e3fbdf8545 to your computer and use it in GitHub Desktop.
adjustDate
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