improve this code "import { formatInTimeZone } from 'date-fns-tz' import {…
- User
improve this code "import { formatInTimeZone } from 'date-fns-tz' import { useLanguageStore, useAuthStore } from '@/stores' import { da, enUS, nb, fi, sv } from 'date-fns/locale' /**
-
In general for dates and time
-
We have agreed to use the format d MMM yyy, HH:mm
-
d for 1 instead of 01 when using MMM as a rule
-
For time we have agreed to use colon : in HH:mm!
-
Try and use what types we have unless you really really need another way of displaying a date */ export const formatTimeRange = ( dateStart: string | Date | undefined, dateEnd?: string | Date | undefined, timeFormat?: boolean ): string => { if (dateStart !== undefined) { // Use app Language for format - as we want it to use the translation for ex. Months, for 'da-DK' - 'June' becomes 'Juni' const storageLocale = localStorage.getItem('appLanguageLocale') || 'en-US' const authStore = useAuthStore() const businessCountry = authStore.member.program?.businessCountry const langStore = useLanguageStore() // Get the current brand to extract the correct locale const getBusinessCountry = langStore.currentBrand?.countries.find( (c) => c.country === businessCountry?.toLowerCase() ) const businessLocale = getBusinessCountry?.locale const start = new Date(dateStart) let formattedString = ''
if (businessLocale !== undefined) { // Format the date for the target timezone - use businessLocale for timezone - so we get the businessCountry timezone const timeZone = getTimeZone(businessLocale) ?? ''
const startDateFormatted = formatInTimeZone(start, timeZone, 'd MMMM yyyy', { locale: localeToDateLocale(storageLocale) })
if (timeFormat !== undefined && timeFormat !== false) { const startTimeFormatted = formatInTimeZone(start, timeZone, 'HH:mm', { locale: localeToDateLocale(storageLocale) }) formattedString =
${startDateFormatted}, ${startTimeFormatted}} else { formattedString =${startDateFormatted}}if (dateEnd !== undefined && timeFormat !== undefined && timeFormat !== false) { const end = new Date(dateEnd) const endTimeFormatted = formatInTimeZone(end, timeZone, 'HH:mm', { locale: localeToDateLocale(storageLocale) })
codeformattedString += ` - ${endTimeFormatted}`}
return formattedString } } return '' }
export const localeToDateLocale = (locale: string) => { switch (locale) { case 'da-DK': return da case 'sv_SE': return sv case 'nb-NO': return nb case 'fi-FI': return fi default: return enUS } }
/** *
- @param bwa abbrevation for before & within & after seen as less then & between two values & higher then
- @param date the supplied date you wish to check
-
- Assistant