Class: OpeningHoursHelper
Helper class for opening hours.
Constructors
Constructor
new OpeningHoursHelper(): OpeningHoursHelper;Returns
OpeningHoursHelper
Methods
checkDayHasSchedule()
static checkDayHasSchedule(openingHours, currentDate?): boolean;Checks if a specific day has a schedule defined in the opening hours.
Parameters
openingHours
An object containing the opening hours for each day of the week.
currentDate?
Date
Optional date to check against. If not provided, uses the current date.
Returns
boolean
true if the day has a schedule defined, false otherwise.
Example
const hours = { mo: [{ open: "09:00", close: "17:00" }] }
const hasSchedule = OpeningHoursHelper.checkDayHasSchedule(hours)
console.log(hasSchedule) // true if today is MondayDefault
Returns false if an error occurs or invalid opening hours are provided.
convertSingleTimeTo12Hour()
static convertSingleTimeTo12Hour(time24h): string;Converts a 24-hour time format to 12-hour format with AM/PM.
Parameters
time24h
string
The time string in 24-hour format (HH:mm).
Returns
string
Time formatted in 12-hour format with AM/PM.
Example
const time12h = OpeningHoursHelper.convertSingleTimeTo12Hour("14:30")
console.log(time12h) // "2:30 p.m."
const morningTime = OpeningHoursHelper.convertSingleTimeTo12Hour("09:00")
console.log(morningTime) // "9:00 a.m."Default
Returns empty string "" if an error occurs or invalid time is provided.flattenOpeningTimes()
static flattenOpeningTimes(openingTimes): OpeningTime;Flattens an array of OpeningTime objects into a single OpeningTime object, taking the earliest open time and the latest close time.
Parameters
openingTimes
The array of OpeningTime objects to flatten.
Returns
A single OpeningTime object representing the flattened time range.
Example
const times = [
{ open: "09:00", close: "12:00" },
{ open: "14:00", close: "18:00" },
]
const flattened = OpeningHoursHelper.flattenOpeningTimes(times)
console.log(flattened) // { open: "09:00", close: "18:00" }Default
Returns { open: "00:00", close: "00:00" } if an error occurs or invalid array is provided.
formatCurrentDaySchedule()
static formatCurrentDaySchedule(openingHours): string;Formats the opening hours for the current day into a string representation.
Parameters
openingHours
The opening hours for the store.
Returns
string
A string representing the opening hours for the current day, or an empty string if there are no opening hours for today.
Example
const hours = { mo: [{ open: "09:00", close: "17:00" }] }
const schedule = OpeningHoursHelper.formatCurrentDaySchedule(hours)
console.log(schedule) // "9:00 a.m. - 5:00 p.m." (if today is Monday)Default
Returns empty string "" if an error occurs or no opening hours for today.formatPreviousDaySchedule()
static formatPreviousDaySchedule(openingHours): string;Formats the opening hours for the previous day into a string representation.
Parameters
openingHours
The opening hours for the store.
Returns
string
A string representing the opening hours for the previous day, or an empty string if there are no opening hours for yesterday.
Example
const hours = { su: [{ open: "10:00", close: "15:00" }] }
const schedule = OpeningHoursHelper.formatPreviousDaySchedule(hours)
console.log(schedule) // "10:00 a.m. - 3:00 p.m." (if yesterday was Sunday)Default
Returns empty string "" if an error occurs or no opening hours for yesterday.formatTimeSchedule()
static formatTimeSchedule(openingTime): string;Formats an opening time schedule into a readable string.
Parameters
openingTime
The opening time object containing open and close times.
Returns
string
Formatted time schedule string.
Example
const schedule = { open: "09:00", close: "17:00" }
const formatted = OpeningHoursHelper.formatTimeSchedule(schedule)
console.log(formatted) // "9:00 a.m. - 5:00 p.m."Default
Returns empty string "" if an error occurs or invalid opening time is provided.getDay()
static getDay(date): null | DaysOfTheWeek;Gets the day of the week as a two-character string indicator.
Parameters
date
Date
The Date object to extract the day from.
Returns
null | DaysOfTheWeek
A DaysOfTheWeek string representing the day of the week.
Example
const date = new Date(2024, 0, 1) // Monday
const day = OpeningHoursHelper.getDay(date)
console.log(day) // "mo"Default
Returns null if an error occurs or invalid date is provided.getDayName()
static getDayName(day): string;Gets the full day name (localized) for a given day string.
Parameters
day
string
The two-character day string (e.g., “mo”, “tu”).
Returns
string
The localized day name, or an empty string if the day is invalid.
Example
const dayName = OpeningHoursHelper.getDayName("mo")
console.log(dayName) // "Monday" (or localized equivalent)
const invalidDay = OpeningHoursHelper.getDayName("invalid")
console.log(invalidDay) // ""Default
Returns empty string "" if an error occurs or invalid day string is provided.getDayNumber()
static getDayNumber(day): number;Gets the day number (1-7) for a given day string.
Parameters
day
string
The two-character day string (e.g., “mo”, “tu”).
Returns
number
The day number (1-7), or -1 if the day is invalid.
Example
const dayNumber = OpeningHoursHelper.getDayNumber("mo")
console.log(dayNumber) // 1
const invalidDay = OpeningHoursHelper.getDayNumber("invalid")
console.log(invalidDay) // -1Default
Returns -1 if an error occurs or invalid day string is provided.getDaysRange()
static getDaysRange(days): string;Converts an array of day abbreviations to a readable day range string.
Parameters
days
string[]
An array of day abbreviations (e.g., [“Mo”, “Tu”, “We”]).
Returns
string
A string representing the day range (e.g., “Monday - Wednesday” for consecutive days, or just “Monday” for a single day).
Example
const range = OpeningHoursHelper.getDaysRange(["Mo", "Tu", "We"])
console.log(range) // "Monday - Wednesday"
const singleDay = OpeningHoursHelper.getDaysRange(["Mo"])
console.log(singleDay) // "Monday"Default
Returns empty string "" if an error occurs or empty array is provided.getNextClosestTime()
static getNextClosestTime(times, currentDate): string;Finds the next closest time from an array of time strings relative to the current date.
Parameters
times
string[]
An array of time strings in HH:mm format (e.g., [“09:00”, “14:30”, “18:00”]).
currentDate
Date
The current date and time to compare against.
Returns
string
The closest upcoming time string, or empty string if no times are provided.
Example
const times = ["09:00", "12:00", "18:00"]
const currentTime = new Date() // Assume it's 10:30 AM
const nextTime = OpeningHoursHelper.getNextClosestTime(times, currentTime)
console.log(nextTime) // "12:00"Default
Returns empty string "" if an error occurs or no times are provided.getNextSchedule()
static getNextSchedule(openingHours, currentDate?): string;Gets the next schedule time (opening or closing) for a place in 12-hour format.
Parameters
openingHours
An object containing the opening hours for each day of the week.
currentDate?
Date
Optional date to check against. If not provided, uses the current date.
Returns
string
The next schedule time in 12-hour format (e.g., “9:00 a.m.” or “6:00 p.m.”).
Example
const hours = { mo: [{ open: "09:00", close: "17:00" }] }
const nextSchedule = OpeningHoursHelper.getNextSchedule(hours)
console.log(nextSchedule) // "9:00 a.m." if closed, "5:00 p.m." if openDefault
Returns "" if an error occurs or invalid opening hours are provided.hasOpeningHoursToday()
static hasOpeningHoursToday(openingHours): boolean;Checks if there are opening times defined for today.
Parameters
openingHours
The opening hours of a store.
Returns
boolean
true if there are opening times for today, false otherwise.
Example
const hours = { mo: [{ open: "09:00", close: "17:00" }] }
const hasHours = OpeningHoursHelper.hasOpeningHoursToday(hours)
console.log(hasHours) // true if today is MondayDefault
Returns false if an error occurs or invalid opening hours are provided.
isOpenFromPreviousDay()
static isOpenFromPreviousDay(openingHours, currentDate?): boolean;Checks if a store is open from the previous day (for overnight hours).
Parameters
openingHours
The opening hours of the store.
currentDate?
Date
Optional date to check against. If not provided, uses the current date.
Returns
boolean
true if the store is open from the previous day, false otherwise.
Example
const overnightHours = { mo: [{ open: "22:00", close: "06:00" }] }
const isOpen = OpeningHoursHelper.isOpenFromPreviousDay(overnightHours)
console.log(isOpen) // true if current time is between 22:00 and 06:00Default
Returns false if an error occurs or invalid opening hours are provided.
isPlaceOpen()
static isPlaceOpen(openingHours, currentDate?): boolean;Checks if a place is currently open based on its opening hours.
Parameters
openingHours
An object containing the opening hours for each day of the week.
currentDate?
Date
Optional date to check against. If not provided, uses the current date.
Returns
boolean
true if the place is currently open, false otherwise.
Example
const hours = { mo: [{ open: "09:00", close: "17:00" }] }
const isOpen = OpeningHoursHelper.isPlaceOpen(hours)
console.log(isOpen) // true if current time is between 09:00 and 17:00 on MondayDefault
Returns false if an error occurs or invalid opening hours are provided.
isValidSchedule()
static isValidSchedule(
openHour,
closeHour,
currentHour): boolean;Checks if the current time is within the specified opening hours.
Parameters
openHour
string
The opening time in HH:mm format (e.g., “09:00”).
closeHour
string
The closing time in HH:mm format (e.g., “18:00”).
currentHour
string
The current time in HH:mm format (e.g., “14:30”).
Returns
boolean
true if the current time is within the opening hours, false otherwise.
Example
const isWithinHours = OpeningHoursHelper.isValidSchedule("09:00", "17:00", "14:30")
console.log(isWithinHours) // true
const isOutsideHours = OpeningHoursHelper.isValidSchedule("09:00", "17:00", "20:00")
console.log(isOutsideHours) // falseDefault
Returns false if an error occurs or invalid time format is provided.
isValidTime()
static isValidTime(time): boolean;Checks if a given string is a valid time in HH:mm format.
Parameters
time
string
The string to validate.
Returns
boolean
true if the string is a valid time, false otherwise.
Example
const isValid = OpeningHoursHelper.isValidTime("14:30")
console.log(isValid) // true
const isInvalid = OpeningHoursHelper.isValidTime("25:00")
console.log(isInvalid) // falseDefault
Returns false if an error occurs or invalid time format is provided.
isWithinOpeningHours()
static isWithinOpeningHours(openingHours, currentDate?): boolean;Checks if the current time is within the opening hours defined for the current day.
Parameters
openingHours
An object containing the opening hours for each day of the week.
currentDate?
Date
Optional date to check against. If not provided, uses the current date.
Returns
boolean
true if the current time is within the opening hours, false otherwise.
Example
const hours = { mo: [{ open: "09:00", close: "17:00" }] }
const isOpen = OpeningHoursHelper.isWithinOpeningHours(hours)
console.log(isOpen) // true if current time is between 09:00 and 17:00 on MondayDefault
Returns false if an error occurs or invalid opening hours are provided.
nextDate()
static nextDate(date): void;Modifies the given Date object to represent the next day.
Parameters
date
Date
The Date object to modify. This object will be mutated.
Returns
void
This function modifies the Date object in place and does not return a value.
Example
const date = new Date(2024, 0, 15) // January 15, 2024
OpeningHoursHelper.nextDate(date)
console.log(date.getDate()) // 16parseTimeAsDate()
static parseTimeAsDate(time, date?): Date;Parses a time string (HH:mm) and combines it with a date to create a new Date object.
Parameters
time
string
The time string in HH:mm format (e.g., “14:30”).
date?
Date
An optional Date object to combine with the time. If not provided, the current date is used.
Returns
Date
A new Date object representing the combined date and time.
Example
const date = new Date(2024, 0, 15)
const timeDate = OpeningHoursHelper.parseTimeAsDate("14:30", date)
console.log(timeDate) // Date object for January 15, 2024 at 2:30 PMDefault
Returns current date if an error occurs or invalid time is provided.parseTimeToDate()
static parseTimeToDate(time, baseDate?): Date;Parses a time string (HH:mm) and creates a Date object for today with that time.
Parameters
time
string
The time string in HH:mm format (e.g., “14:30”).
baseDate?
Date
Optional base date to use. If not provided, uses the current date.
Returns
Date
A Date object representing the date with the specified time.
Example
const timeDate = OpeningHoursHelper.parseTimeToDate("14:30")
console.log(timeDate) // Date object for today at 2:30 PMDefault
Returns current date if an error occurs or invalid time is provided.previousDate()
static previousDate(date): void;Modifies the given Date object to represent the previous day.
Parameters
date
Date
The Date object to modify. This object will be mutated.
Returns
void
This function modifies the Date object in place and does not return a value.
Example
const date = new Date(2024, 0, 15) // January 15, 2024
OpeningHoursHelper.previousDate(date)
console.log(date.getDate()) // 14sortDays()
static sortDays(openingHours): Record<string, OpeningTime[]>;Sorts the opening hours object by day of the week.
Parameters
openingHours
The opening hours object to sort.
Returns
Record<string, OpeningTime[]>
A new object with the opening hours sorted by day of the week.
Example
const unsortedHours = { fr: [...], mo: [...], we: [...] };
const sortedHours = OpeningHoursHelper.sortDays(unsortedHours);
console.log(Object.keys(sortedHours)); // ["mo", "we", "fr"]Default
Returns the original openingHours object if an error occurs.sortOpeningTimesByOpen()
static sortOpeningTimesByOpen(openingTimes): OpeningTime[];Sorts a list of OpeningTime objects in place, based on the ‘open’ property.
This method mutates the array and returns a reference to the same array.
Parameters
openingTimes
The array of OpeningTime objects to sort. This array will be mutated.
Returns
The sorted array of OpeningTime objects (mutated in place).
Example
const times = [
{ open: "14:00", close: "18:00" },
{ open: "09:00", close: "12:00" },
]
const sorted = OpeningHoursHelper.sortOpeningTimesByOpen(times)
console.log(sorted[0].open) // "09:00"Default
Returns the original array if an error occurs.toMinutes()
static toMinutes(time): number;Converts a time string (HH:mm) to total minutes since midnight.
Parameters
time
string
The time string in HH:mm format (e.g., “14:30”).
Returns
number
The total number of minutes since midnight (e.g., 870 for “14:30”).
Example
const minutes = OpeningHoursHelper.toMinutes("14:30")
console.log(minutes) // 870
const morningMinutes = OpeningHoursHelper.toMinutes("09:00")
console.log(morningMinutes) // 540Default
Returns 0 if an error occurs or invalid time format is provided.