Last active
April 20, 2021 11:51
-
-
Save mmarienko/27fb6bf9248d1b15f2b4a2ce528053d9 to your computer and use it in GitHub Desktop.
Format Date with internationalization
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
function formatDate(date) { | |
let year, month, day; | |
let yearIntl = IntlTime.formatToParts(date.getYear() - 70, 'year'); | |
let monthIntl = IntlTime.formatToParts(date.getMonth(), 'month'); | |
let dayIntl = IntlTime.formatToParts(date.getDate(), 'day'); | |
year = date.getYear() - 70 ? (yearIntl[1] ? yearIntl[1].value + yearIntl[2].value + ',' : yearIntl[0].value + ',') : ''; | |
month = +date.getMonth() ? (monthIntl[1] ? monthIntl[1].value + monthIntl[2].value + ',' : monthIntl[0].value + ',') : ''; | |
if ((new Date(date.getYear(), date.getMonth(), date.getDate() + 1)).getDate() != 1) { | |
day = dayIntl[1] ? dayIntl[1].value + dayIntl[2].value : dayIntl[0].value; | |
} else if ((date.getMonth() + 1) != 12) { | |
day = ''; | |
monthIntl = IntlTime.formatToParts(date.getMonth() + 1, 'month'); | |
month = monthIntl[1] ? +monthIntl[1].value + monthIntl[2].value : monthIntl[0].value; | |
} else { | |
day = month = ''; | |
yearIntl = IntlTime.formatToParts(date.getYear() - 69, 'year'); | |
year = yearIntl[1] ? +yearIntl[1].value + yearIntl[2].value : yearIntl[0].value; | |
} | |
return `${year} ${month} ${day}`; | |
} | |
/** Example | |
formatDate(new Date(90 * 86400000) - 86400000); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment