add some date handling

This commit is contained in:
2024-05-12 01:40:59 +02:00
parent a6fcaa1579
commit e12a9a82fa
6 changed files with 76 additions and 10 deletions

24
web/src/lib/date.ts Normal file
View File

@ -0,0 +1,24 @@
import { isToday, isAfter, parseISO } from "date-fns";
import { toZonedTime, format } from "date-fns-tz";
const timeZone = "Europe/Oslo";
export const commonDateFormat = "dd.MM.yyyy 'kl.' HH:mm";
export function toLocalTime(date: Date | string | number) {
return toZonedTime(date, timeZone);
}
export function formatDate(date: Date | string | number, formatStr: string) {
return format(toLocalTime(date), formatStr, { timeZone });
}
export function isTodayOrFuture(
date: Date | string | number,
timeZone = "UTC"
) {
const now = new Date();
const zonedNow = toZonedTime(now, timeZone);
const zonedDate = toLocalTime(date);
return isToday(zonedDate) || isAfter(zonedDate, zonedNow);
}

View File

@ -1,6 +1,8 @@
import { graphql } from "@/gql";
import { EventFragment, EventOccurrence } from "@/gql/graphql";
export type { EventFragment } from "@/gql/graphql"
const EventFragmentDefinition = graphql(`
fragment Event on EventPage {
__typename