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)
        })

        formattedString += ` - ${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
 * @param time what value you wish to calculate for. written as { milliseconds, seconds, hours, days, years } choose one of them days & hours & years do not need to be used unless you wish to check against a specific hour, day or year.
 * @param hours how many hours you wish to check against can be nothing. Write as whole number eg 7
 * @param days how many days you wish to check against can be nothing. Write as a day so 1-31
 * @param years how many years you wish to check against can be nothing. Write this as a whole year so eg. 3
 *
 * ! Important if multiple checks need to be handled call method again
 * @returns
 */

export const isDateBeforeAfterWithin = (
  bwa: string,
  date: string,
  time: string,
  hours?: number,
  days?: number,
  years?: number
): boolean => {
  // written in miliseconds equal to one of each timestamp
  const hour = 3600000
  const day = 86400000
  const year = 31556952000

  // Convert date to string
  const suppliedDate = new Date(date.toString()).getTime()

  // Get the current date and time
  const currentDate = new Date().getTime()

  // get the positive time in miliseconds between two dates
  const timeInMiliseconds = Math.abs(suppliedDate - currentDate)
  // The difference between the date checked against and the date provided divded by however long one needs to check against
  let targetTime = 0

  //Calculate the difference between the date supplied and today's
  switch (time) {
    case 'hours':
      targetTime = hours! * hour
      break
    case 'days':
      targetTime = days! * day
      break
    case 'years':
      targetTime = years! * year
      break
  }

  switch (bwa) {
    case 'before':
      return targetTime <= timeInMiliseconds
    case 'within':
      return targetTime >= timeInMiliseconds && targetTime <= timeInMiliseconds
    case 'after':
      return targetTime >= timeInMiliseconds
  }

  return false
}

type LocaleTimezoneMap = {
  [locale: string]: string
}

const localeTimezoneMap: LocaleTimezoneMap = {
  'fi-FI': 'Europe/Helsinki',
  'sv-SE': 'Europe/Stockholm',
  'da-DK': 'Europe/Copenhagen',
  'nb-NO': 'Europe/Oslo',
  'nn-NO': 'Europe/Oslo'
}

const getTimeZone = (locale: string): string | undefined => {
  return localeTimezoneMap[locale]
}
"