Skip to content

Commit

Permalink
feat: meteo page
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Jan 19, 2025
1 parent 14e63bf commit 1cf0877
Show file tree
Hide file tree
Showing 18 changed files with 625 additions and 46 deletions.
8 changes: 4 additions & 4 deletions src/akte/date.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const format = {
us: new Intl.DateTimeFormat("en-US", {
usDate: new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "2-digit",
day: "2-digit",
}),
} as const

export function dateToUSFormat(rawDate: string | number): string {
export function dateToUSDate(rawDate: string | number): string {
const date = new Date(rawDate)

return format.us.format(date)
return format.usDate.format(date)
}

export function dateToISOFormat(rawDate: string | number): string {
export function dateToISO(rawDate: string | number): string {
const date = new Date(rawDate)

return date.toISOString().replace(/\.\d\d\dZ$/, "+00:00")
Expand Down
4 changes: 2 additions & 2 deletions src/assets/js/albums.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { read } from "./lib/albums"
import { dateToUSFormat } from "./lib/format"
import { dateToUSDate } from "./lib/format"
import "./_base"

const albums = read()
Expand All @@ -15,7 +15,7 @@ if ($main) {
return /* html */ `
<li class="flex gap-2">
<time datetime="${album.date}" class="ff-numeric">
${dateToUSFormat(album.date)}
${dateToUSDate(album.date)}
</time>
<a href="/albums/${album.slug}" class="lowercase underline">
${album.title}
Expand Down
50 changes: 46 additions & 4 deletions src/assets/js/lib/format.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,63 @@
const dateFormat = {
us: new Intl.DateTimeFormat("en-US", {
usDate: new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "2-digit",
day: "2-digit",
}),
usDay: new Intl.DateTimeFormat("en-US", {
weekday: "long",
}),
usTime: new Intl.DateTimeFormat("en-US", {
hour: "2-digit",
minute: "2-digit",
hour12: false,
}),
usRelativeDays: new Intl.RelativeTimeFormat("en-US", {
numeric: "auto",
}),
} as const

export function dateToUSFormat(rawDate: string | number): string {
export function dateToUSDate(rawDate: string | number): string {
const date = new Date(rawDate)

return dateFormat.usDate.format(date)
}

export function dateToUSDay(rawDate: string | number): string {
const date = new Date(rawDate)

return dateFormat.usDay.format(date)
}

export function dateToUSTime(rawDate: string | number): string {
const date = new Date(rawDate)

return dateFormat.us.format(date)
return dateFormat.usTime.format(date)
}

const ONE_DAY_MS = 1000 * 60 * 60 * 24
export function dateToUSRelativeDays(rawDate: string | number): string {
const date = new Date(rawDate)
const time = date.getTime()
const to = time - (time % ONE_DAY_MS)

const now = Date.now()
const today = now - (now % ONE_DAY_MS)

const diff = to - today
const days = Math.floor(diff / ONE_DAY_MS)

if (days >= 2) {
return dateToUSDay(rawDate)
}

return dateFormat.usRelativeDays.format(days, "day")
}

const numberFormat = {
us: new Intl.NumberFormat("en-US"),
} as const

export function numberToUSFormat(rawNumber: number): string {
export function numberToUS(rawNumber: number): string {
return numberFormat.us.format(rawNumber)
}
Loading

0 comments on commit 1cf0877

Please sign in to comment.