-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
22 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { curry } from './curry'; | ||
import { parseDate } from './parseDate'; | ||
|
||
export const createDateFormatter = curry((locale: string, date: Date | string) => { | ||
return date ? new Intl.DateTimeFormat(locale).format(new Date(date)) : null; | ||
export const createDateFormatter = curry((locale: string, value: Date | string) => { | ||
const date = parseDate(value); | ||
return date ? new Intl.DateTimeFormat(locale).format(date) : value; | ||
}); | ||
|
||
export const formatBRDate = createDateFormatter('pt-BR'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { isString } from './isString'; | ||
|
||
export function parseDate(date?: unknown) { | ||
if (date instanceof Date) return date; | ||
if (isString(date)) { | ||
let arr; | ||
const rx1 = /^(\d{4})-(\d{2})-(\d{2})/; | ||
const rx2 = /^(\d{2})\/(\d{2})\/(\d{4})/; | ||
const getDate = ([y, m, d]: string[]) => { | ||
const val = new Date(~~y, ~~m - 1, ~~d, 0, 0, 0); | ||
return ~~m <= 12 && val.getTime() ? val : null; | ||
}; | ||
if ((arr = date.match(rx1))) return getDate(arr.slice(1, 4)); | ||
if ((arr = date.match(rx2))) return getDate(arr.slice(1, 4).reverse()); | ||
} | ||
return null; | ||
} |