Skip to content

Commit e3324d3

Browse files
Add date-fns 3 support
1 parent 9b49671 commit e3324d3

File tree

6 files changed

+18
-50
lines changed

6 files changed

+18
-50
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Next
44
- Add MMM format to allow short month names (@peterbell215)
5+
- Add date-fns 3 support
56

67
## 2.12.0 - 2024 Apr 9
78
- Disable autocomplete for date input (@gianarb)

package-lock.json

Lines changed: 6 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@typescript-eslint/eslint-plugin": "^7.10.0",
2424
"@typescript-eslint/parser": "^7.10.0",
2525
"@vitest/coverage-v8": "^1.6.0",
26-
"date-fns": "^2.30.0",
26+
"date-fns": "^3.6.0",
2727
"eslint": "^8.57.0",
2828
"eslint-config-prettier": "^9.1.0",
2929
"eslint-plugin-svelte": "^2.39.0",

src/lib/locale.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,16 @@ export function getInnerLocale(locale: Locale): InnerLocale {
5555
return innerLocale
5656
}
5757

58+
type Month = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
59+
type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6
60+
type LocaleWidth = 'short' | 'wide' | 'abbreviated' | 'narrow' | 'any'
5861
type DateFnsLocale = {
5962
options?: {
6063
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6
6164
}
6265
localize?: {
63-
month: (n: number, options?: { width?: string }) => string
64-
day: (i: number, options?: { width?: string }) => string
66+
month: (n: Month, options?: { width?: LocaleWidth }) => string
67+
day: (i: Day, options?: { width?: LocaleWidth }) => string
6568
}
6669
}
6770
/** Create a Locale from a date-fns locale */
@@ -73,12 +76,12 @@ export function localeFromDateFnsLocale(dateFnsLocale: DateFnsLocale): InnerLoca
7376
if (dateFnsLocale.localize) {
7477
for (let i = 0; i < 7; i++) {
7578
// widths: narrow, short, abbreviated, wide, any
76-
locale.weekdays[i] = dateFnsLocale.localize.day(i, { width: 'short' })
79+
locale.weekdays[i] = dateFnsLocale.localize.day(i as Day, { width: 'short' })
7780
}
7881

7982
for (let i = 0; i < 12; i++) {
80-
locale.months[i] = dateFnsLocale.localize.month(i, { width: 'wide' })
81-
locale.shortMonths[i] = dateFnsLocale.localize.month(i, { width: 'abbreviated' })
83+
locale.months[i] = dateFnsLocale.localize.month(i as Month, { width: 'wide' })
84+
locale.shortMonths[i] = dateFnsLocale.localize.month(i as Month, { width: 'abbreviated' })
8285
}
8386
}
8487
return locale

src/routes/DateInput.svelte

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
import Prop from './prop.svelte'
44
import Split from './split.svelte'
55
import { localeFromDateFnsLocale } from '$lib'
6-
7-
// had to import it this way to avoid errors
8-
// in `npm run build:site` or `npm run check`:
9-
import hy from 'date-fns/locale/hy/index'
10-
import de from 'date-fns/locale/de/index'
11-
import nb from 'date-fns/locale/nb/index'
6+
import { hy, de, nb } from 'date-fns/locale'
127
138
let id: string
149
let placeholder: string

src/routes/DatePicker.svelte

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
import { localeFromDateFnsLocale } from '$lib/locale.js'
44
import Prop from './prop.svelte'
55
import Split from './split.svelte'
6-
7-
// had to import it this way to avoid errors
8-
// in `npm run build:site` or `npm run check`:
9-
import hy from 'date-fns/locale/hy/index'
10-
import de from 'date-fns/locale/de/index'
11-
import nb from 'date-fns/locale/nb/index'
6+
import { hy, de, nb } from 'date-fns/locale'
127
138
let value: Date
149
let min: Date

0 commit comments

Comments
 (0)