Skip to content

Commit 404c277

Browse files
MathildeJollymjolly
andauthored
feat: add format second to formatDate (#2522)
* feat: add format second to formatDate --------- Co-authored-by: mjolly <[email protected]>
1 parent 6e78166 commit 404c277

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

.changeset/honest-bananas-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@scaleway/use-i18n": minor
3+
---
4+
5+
feat: add format second to formatDate

packages/use-i18n/src/__tests__/__snapshots__/formatDate.test.ts.snap

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,36 @@ exports[`formatDate > should work with format "numericHour", for date = "new Dat
186186

187187
exports[`formatDate > should work with format "numericHour", for date = "new Date(2020, 1, 13, 16, 28)" and locale "ro" 1`] = `"2020-02-13 16:28"`;
188188

189+
exports[`formatDate > should work with format "second", for date = "2020-02-13T15:28:00.000Z" and locale "de" 1`] = `"13. Februar 2020 um 15:28:00"`;
190+
191+
exports[`formatDate > should work with format "second", for date = "2020-02-13T15:28:00.000Z" and locale "en" 1`] = `"February 13, 2020 at 3:28:00 PM"`;
192+
193+
exports[`formatDate > should work with format "second", for date = "2020-02-13T15:28:00.000Z" and locale "es" 1`] = `"13 de febrero de 2020, 15:28:00"`;
194+
195+
exports[`formatDate > should work with format "second", for date = "2020-02-13T15:28:00.000Z" and locale "fr" 1`] = `"13 février 2020 à 15:28:00"`;
196+
197+
exports[`formatDate > should work with format "second", for date = "2020-02-13T15:28:00.000Z" and locale "ro" 1`] = `"13 februarie 2020 la 15:28:00"`;
198+
199+
exports[`formatDate > should work with format "second", for date = "1581607680000" and locale "de" 1`] = `"13. Februar 2020 um 15:28:00"`;
200+
201+
exports[`formatDate > should work with format "second", for date = "1581607680000" and locale "en" 1`] = `"February 13, 2020 at 3:28:00 PM"`;
202+
203+
exports[`formatDate > should work with format "second", for date = "1581607680000" and locale "es" 1`] = `"13 de febrero de 2020, 15:28:00"`;
204+
205+
exports[`formatDate > should work with format "second", for date = "1581607680000" and locale "fr" 1`] = `"13 février 2020 à 15:28:00"`;
206+
207+
exports[`formatDate > should work with format "second", for date = "1581607680000" and locale "ro" 1`] = `"13 februarie 2020 la 15:28:00"`;
208+
209+
exports[`formatDate > should work with format "second", for date = "new Date(2020, 1, 13, 16, 28)" and locale "de" 1`] = `"13. Februar 2020 um 16:28:00"`;
210+
211+
exports[`formatDate > should work with format "second", for date = "new Date(2020, 1, 13, 16, 28)" and locale "en" 1`] = `"February 13, 2020 at 4:28:00 PM"`;
212+
213+
exports[`formatDate > should work with format "second", for date = "new Date(2020, 1, 13, 16, 28)" and locale "es" 1`] = `"13 de febrero de 2020, 16:28:00"`;
214+
215+
exports[`formatDate > should work with format "second", for date = "new Date(2020, 1, 13, 16, 28)" and locale "fr" 1`] = `"13 février 2020 à 16:28:00"`;
216+
217+
exports[`formatDate > should work with format "second", for date = "new Date(2020, 1, 13, 16, 28)" and locale "ro" 1`] = `"13 februarie 2020 la 16:28:00"`;
218+
189219
exports[`formatDate > should work with format "short", for date = "2020-02-13T15:28:00.000Z" and locale "de" 1`] = `"13. Feb. 2020"`;
190220

191221
exports[`formatDate > should work with format "short", for date = "2020-02-13T15:28:00.000Z" and locale "en" 1`] = `"Feb 13, 2020"`;

packages/use-i18n/src/__tests__/formatDate.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('formatDate', () => {
7373
// @ts-expect-error we check a failing case
7474
formatDate('fr', 1581607680000, 'not a valid format'),
7575
).toThrowError(
76-
'format "not a valid format" should be one of hour, hourOnly, long, short, shortWithoutDay, numeric, numericHour or a valid Intl.DateTimeFormat options object',
76+
'format "not a valid format" should be one of second, hour, hourOnly, long, short, shortWithoutDay, numeric, numericHour or a valid Intl.DateTimeFormat options object',
7777
)
7878
})
7979
})

packages/use-i18n/src/formatDate.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { formatISO, intlFormat } from 'date-fns'
22

33
const formatOptions = {
4+
second: {
5+
// Expected output format: 13 February 2025 at 4:28:00
6+
day: 'numeric',
7+
month: 'long',
8+
year: 'numeric',
9+
hour: 'numeric',
10+
minute: 'numeric',
11+
second: 'numeric',
12+
},
413
hour: {
514
// Expected output format: February 13, 2020, 4:28 PM
615
day: 'numeric',

0 commit comments

Comments
 (0)