Skip to content

Instantly share code, notes, and snippets.

@williamkoller
Last active January 12, 2024 14:05
Show Gist options
  • Save williamkoller/0535cb0df700f6df4a7a3b816a9be818 to your computer and use it in GitHub Desktop.
Save williamkoller/0535cb0df700f6df4a7a3b816a9be818 to your computer and use it in GitHub Desktop.
export const formatDateToBR = (date: Date): string => {
return new Intl.DateTimeFormat('pt-BR').format(date)
}
export const formatDateToISO = (date: Date): string => {
return date.toDateString().split('T')[0]
}
export const formatDateToBRShort = (date: Date): string => {
return new Intl.DateTimeFormat('pr-BR', {
day: '2-digit',
month: '2-digit',
}).format(date)
}
export const formatDateToBRWithoutYear = (date: Date): string => {
return new Intl.DateTimeFormat('pt-BR', {
day: '2-digit',
month: 'long'
}).format(date)
}
const shortDateFormatters = {
formatToBR: formatDateToBR,
formatToISO: formatDateToISO,
formatToBRWithout: formatDateToBRWithoutYear,
formatToBRShort: formatDateToBRShort
}
const currentDate = new Date()
console.log({
formatToBR: shortDateFormatters.formatToBR(currentDate),
formatToISO: shortDateFormatters.formatToISO(currentDate),
formatToBRWithoutYear: shortDateFormatters.formatToBRWithout(currentDate),
formatToBRShort: shortDateFormatters.formatToBRShort(currentDate)
})
{
formatToBR: '12/01/2024',
formatToISO: '2024-01-12',
formatToBRWithoutYear: '12 de janeiro',
formatToBRShort: '12/01'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment