Skip to content

Commit

Permalink
rework date comparison and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rtrembecky committed Nov 22, 2024
1 parent 45fab45 commit f8dcc20
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
21 changes: 10 additions & 11 deletions src/components/CompetitionPage/UpcomingOrCurrentEventInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {DateTime} from 'luxon'
import {FC} from 'react'

import {Event} from '@/types/api/competition'
import {formatDateTime, formatDateTimeInterval} from '@/utils/formatDate'
import {DateFormat, formatDateTimeInterval} from '@/utils/formatDate'

import {Link} from '../Clickable/Link'

Expand All @@ -12,22 +12,21 @@ export const UpcomingOrCurrentEventInfo: FC<{event: Event; name: string; shortNa
name,
shortName,
}) => {
const {year, school_year, location, registration_link, publication_set} = event
const {year, school_year, location, registration_link, publication_set, start, end} = event

const upcomingEventDate = event ? formatDateTimeInterval(event.start, event.end) : null
const upcomingEventDate = event ? formatDateTimeInterval(start, end) : null

const regStart = DateTime.fromISO(registration_link.start)
const regEnd = DateTime.fromISO(registration_link.end)
const now = DateTime.now()

const registrationInfo = (() => {
if (DateTime.fromISO(registration_link.start) > DateTime.now())
return `Registrácia bude otvorená od ${formatDateTime(registration_link.start)}`
else if (DateTime.fromISO(registration_link.end) > DateTime.now())
return `Registrácia je otvorená do ${formatDateTime(registration_link.end)}`
if (now < regStart) return `Registrácia bude otvorená od ${regStart.toFormat(DateFormat.DATE_TIME)}`
if (now < regEnd) return `Registrácia je otvorená do ${regEnd.toFormat(DateFormat.DATE_TIME)}`
return `Registrácia bola ukončená`
})()

const isRegistrationActive = registration_link
? DateTime.fromISO(registration_link.start) < DateTime.now() &&
DateTime.fromISO(registration_link.end) > DateTime.now()
: false
const isRegistrationActive = regStart < now && regEnd > now

return (
<Stack gap={1}>
Expand Down
27 changes: 19 additions & 8 deletions src/utils/formatDate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import {DateTime} from 'luxon'

export const formatDateTime = (date: string) => DateTime.fromISO(date).toFormat('dd. MM. yyyy HH:mm')
export const formatDate = (date: string) => DateTime.fromISO(date).toFormat('dd. LLL yyyy')
export const formatDateDigits = (date: string) => DateTime.fromISO(date).toFormat('dd. MM. yyyy')
export enum DateFormat {
TIME = 'HH:mm',
DATE = 'dd. LLL yyyy',
DATE_TIME = 'dd. MM. yyyy HH:mm',
DATE_DIGITS = 'dd. MM. yyyy',
}

export const formatDateTime = (date: string) => DateTime.fromISO(date).toFormat(DateFormat.DATE_TIME)
export const formatDate = (date: string) => DateTime.fromISO(date).toFormat(DateFormat.DATE)
export const formatDateDigits = (date: string) => DateTime.fromISO(date).toFormat(DateFormat.DATE_DIGITS)

export function formatDateTimeInterval(date1: string, date2: string) {
if (date1 === date2) return formatDateDigits(date1)
const dateTime1 = DateTime.fromISO(date1)
const dateTime2 = DateTime.fromISO(date2)
if (dateTime1 === dateTime2) return formatDate(date1)
if (dateTime1.toFormat('dd.MM.yyyy') === dateTime2.toFormat('dd.MM.yyyy'))
return `${dateTime1.toFormat('dd.MM.yyyy')} ${dateTime1.toFormat('HH:mm')} - ${dateTime2.toFormat('HH:mm')}`
return `${formatDateTime(date1)} - ${formatDateTime(date2)}`

if (dateTime1.equals(dateTime2)) return formatDateDigits(date1)

if (dateTime1.startOf('day').equals(dateTime2.startOf('day')))
return `${dateTime1.toFormat(DateFormat.DATE_DIGITS)} ${dateTime1.toFormat(DateFormat.TIME)} - ${dateTime2.toFormat(
DateFormat.TIME,
)}`

return `${dateTime1.toFormat(DateFormat.DATE_TIME)} - ${dateTime2.toFormat(DateFormat.DATE_TIME)}`
}

0 comments on commit f8dcc20

Please sign in to comment.