I'am getting a response from backend with date in this fromat created_date: "2021-09-27T06:10:07.972531Z", but I want to show such as 31 December,2020 in the UI. So is there any way to get only year,month and the day?
CodePudding user response:
Take a look at this answer: https://stackoverflow.com/a/69356761/4633925
Intl is the best approach here
const date = new Date("2021-09-27T06:10:07.972531Z");
const formattedDate = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' }).format(date)
This will give you Sep 27, 2021
go through the docs and find the best formatting option for your needs.
CodePudding user response:
Please use the [Moment.js] npm package.
Then import it and use the below code in react component.
import moment from "moment";
let createdAt = "2021-09-27T06:10:07.972531Z";
{moment(createdAt).format("Do MMMM, YYYY")}
CodePudding user response:
Without any external dependency it can be done:
new Date('2021-09-27T06:10:07.972531Z')
.toLocaleDateString('en-US', { day: 'numeric', month: 'long', year: 'numeric' } )
> 'September 27, 2021'
By specifying the appropriate locale
you will have different terms order. In your specific case the most appropriate seems en-IN
or en-AU
:
new Date('2021-09-27T06:10:07.972531Z')
.toLocaleDateString('en-IN', { day: 'numeric', month: 'long', year: 'numeric' } )
> '27 September 2021'
Regarding the comma present here 31 December, 2020
I'm not aware of any locale that includes the comma, you may try your exact locale (if this is how dates are written in your country)
This is an example of all the locale Date formats available in javascript, according to this answer
{
"af-ZA": "27 settembre 2021",
"am-ET": "27 ሴፕቴምበር 2021",
"ar-AE": "٢٧ سبتمبر ٢٠٢١",
"ar-BH": "٢٧ سبتمبر ٢٠٢١",
"ar-DZ": "27 سبتمبر 2021",
"ar-EG": "٢٧ سبتمبر ٢٠٢١",
"ar-IQ": "٢٧ أيلول ٢٠٢١",
"ar-JO": "٢٧ أيلول ٢٠٢١",
"ar-KW": "٢٧ سبتمبر ٢٠٢١",
"ar-LB": "٢٧ أيلول ٢٠٢١",
"ar-LY": "27 سبتمبر 2021",
"ar-MA": "27 شتنبر 2021",
"ar-OM": "٢٧ سبتمبر ٢٠٢١",
"ar-QA": "٢٧ سبتمبر ٢٠٢١",
"ar-SA": "٢٠ صفر ١٤٤٣ هـ",
"ar-SY": "٢٧ أيلول ٢٠٢١",
"ar-TN": "27 سبتمبر 2021",
"ar-YE": "٢٧ سبتمبر ٢٠٢١",
"arn-CL": "27 settembre 2021",
"as-IN": "27 settembre 2021",
"az-Cyrl-AZ": "27 сентјабр 2021",
"az-Latn-AZ": "2021 M09 27",
"ba-RU": "27 settembre 2021",
"be-BY": "27 settembre 2021",
"bg-BG": "27 септември 2021 г.",
"bn-BD": "২৭ সেপ্টেম্বর, ২০২১",
"bn-IN": "২৭ সেপ্টেম্বর, ২০২১",
"bo-CN": "27 settembre 2021",
"br-FR": "27 settembre 2021",
"bs-Cyrl-BA": "27. септембар 2021.",
"bs-Latn-BA": "2021 M09 27",
"ca-ES": "27 de setembre de 2021",
"co-FR": "27 settembre 2021",
"cs-CZ": "27. září 2021",
"cy-GB": "27 settembre 2021",
"da-DK": "27. september 2021",
"de-AT": "27. September 2021",
"de-CH": "27. September 2021",
"de-DE": "27. September 2021",
"de-LI": "27. September 2021",
"de-LU": "27. September 2021",
"dsb-DE": "27 settembre 2021",
"dv-MV": "27 settembre 2021",
"el-GR": "27 Σεπτεμβρίου 2021",
"en-029": "September 27, 2021",
"en-AU": "27 September 2021",
"en-BZ": "27 September 2021",
"en-CA": "September 27, 2021",
"en-GB": "27 September 2021",
"en-IE": "27 September 2021",
"en-IN": "27 September 2021",
"en-JM": "27 September 2021",
"en-MY": "27 September 2021",
"en-NZ": "27 September 2021",
"en-PH": "27 September 2021",
"en-SG": "27 September 2021",
"en-TT": "27 September 2021",
"en-US": "September 27, 2021",
"en-ZA": "27 September 2021",
"en-ZW": "27 September 2021",
"es-AR": "27 de septiembre de 2021",
"es-BO": "27 de septiembre de 2021",
"es-CL": "27 de septiembre de 2021",
"es-CO": "27 de septiembre de 2021",
"es-CR": "27 de septiembre de 2021",
"es-DO": "27 de septiembre de 2021",
"es-EC": "27 de septiembre de 2021",
"es-ES": "27 de septiembre de 2021",
"es-GT": "27 de septiembre de 2021",
"es-HN": "27 de septiembre de 2021",
"es-MX": "27 de septiembre de 2021",
"es-NI": "27 de septiembre de 2021",
"es-PA": "27 de septiembre de 2021",
"es-PE": "27 de setiembre de 2021",
"es-PR": "27 de septiembre de 2021",
"es-PY": "27 de septiembre de 2021",
"es-SV": "27 de septiembre de 2021",
"es-US": "27 de septiembre de 2021",
"es-UY": "27 de setiembre de 2021",
"es-VE": "27 de septiembre de 2021",
"et-EE": "27. september 2021",
"eu-ES": "27 settembre 2021",
"fa-IR": "۵ مهر ۱۴۰۰",
"fi-FI": "27. syyskuuta 2021",
"fil-PH": "Setyembre 27, 2021",
"fo-FO": "27 settembre 2021",
"fr-BE": "27 septembre 2021",
"fr-CA": "27 septembre 2021",
"fr-CH": "27 septembre 2021",
"fr-FR": "27 septembre 2021",
"fr-LU": "27 septembre 2021",
"fr-MC": "27 septembre 2021",
"fy-NL": "27 settembre 2021",
"ga-IE": "27 settembre 2021",
"gd-GB": "27 settembre 2021",
"gl-ES": "27 settembre 2021",
"gsw-FR": "27 settembre 2021",
"gu-IN": "27 સપ્ટેમ્બર, 2021",
"ha-Latn-NG": "27 settembre 2021",
"he-IL": "27 בספטמבר 2021",
"hi-IN": "27 सितंबर 2021",
"hr-BA": "27. rujna 2021.",
"hr-HR": "27. rujna 2021.",
"hsb-DE": "27 settembre 2021",
"hu-HU": "2021. szeptember 27.",
"hy-AM": "27 settembre 2021",
"id-ID": "27 September 2021",
"ig-NG": "27 settembre 2021",
"ii-CN": "27 settembre 2021",
"is-IS": "27 settembre 2021",
"it-CH": "27 settembre 2021",
"it-IT": "27 settembre 2021",
"iu-Cans-CA": "27 settembre 2021",
"iu-Latn-CA": "27 settembre 2021",
"ja-JP": "2021年9月27日",
"ka-GE": "27 settembre 2021",
"kk-KZ": "27 settembre 2021",
"kl-GL": "27 settembre 2021",
"km-KH": "27 settembre 2021",
"kn-IN": "ಸೆಪ್ಟೆಂಬರ್ 27, 2021",
"ko-KR": "2021년 9월 27일",
"kok-IN": "27 settembre 2021",
"ky-KG": "27 settembre 2021",
"lb-LU": "27 settembre 2021",
"lo-LA": "27 settembre 2021",
"lt-LT": "2021 m. rugsėjo 27 d.",
"lv-LV": "2021. gada 27. septembris",
"mi-NZ": "27 settembre 2021",
"mk-MK": "27 settembre 2021",
"ml-IN": "2021, സെപ്റ്റംബർ 27",
"mn-MN": "27 settembre 2021",
"mn-Mong-CN": "27 settembre 2021",
"moh-CA": "27 settembre 2021",
"mr-IN": "२७ सप्टेंबर, २०२१",
"ms-BN": "27 September 2021",
"ms-MY": "27 September 2021",
"mt-MT": "27 settembre 2021",
"nb-NO": "27. september 2021",
"ne-NP": "27 settembre 2021",
"nl-BE": "27 september 2021",
"nl-NL": "27 september 2021",
"nn-NO": "27 settembre 2021",
"nso-ZA": "27 settembre 2021",
"oc-FR": "27 settembre 2021",
"or-IN": "27 settembre 2021",
"pa-IN": "2021 M09 27",
"pl-PL": "27 września 2021",
"prs-AF": "۵ میزان ۱۴۰۰",
"ps-AF": "27 settembre 2021",
"pt-BR": "27 de setembro de 2021",
"pt-PT": "27 de setembro de 2021",
"qut-GT": "27 settembre 2021",
"quz-BO": "27 settembre 2021",
"quz-EC": "27 settembre 2021",
"quz-PE": "27 settembre 2021",
"rm-CH": "27 settembre 2021",
"ro-RO": "27 septembrie 2021",
"ru-RU": "27 сентября 2021 г.",
"rw-RW": "27 settembre 2021",
"sa-IN": "27 settembre 2021",
"sah-RU": "27 settembre 2021",
"se-FI": "27 settembre 2021",
"se-NO": "27 settembre 2021",
"se-SE": "27 settembre 2021",
"si-LK": "27 settembre 2021",
"sk-SK": "27. septembra 2021",
"sl-SI": "27. september 2021",
"sma-NO": "27 settembre 2021",
"sma-SE": "27 settembre 2021",
"smj-NO": "27 settembre 2021",
"smj-SE": "27 settembre 2021",
"smn-FI": "27 settembre 2021",
"sms-FI": "27 settembre 2021",
"sq-AL": "27 settembre 2021",
"sr-Cyrl-BA": "27. септембар 2021.",
"sr-Cyrl-CS": "27. септембар 2021.",
"sr-Cyrl-ME": "27. септембар 2021.",
"sr-Cyrl-RS": "27. септембар 2021.",
"sr-Latn-BA": "27. septembar 2021.",
"sr-Latn-CS": "27. septembar 2021.",
"sr-Latn-ME": "27. septembar 2021.",
"sr-Latn-RS": "27. septembar 2021.",
"sv-FI": "27 september 2021",
"sv-SE": "27 september 2021",
"sw-KE": "27 Septemba 2021",
"syr-SY": "27 settembre 2021",
"ta-IN": "27 செப்டம்பர், 2021",
"te-IN": "27 సెప్టెంబర్, 2021",
"tg-Cyrl-TJ": "27 settembre 2021",
"th-TH": "27 กันยายน 2564",
"tk-TM": "27 settembre 2021",
"tn-ZA": "27 settembre 2021",
"tr-TR": "27 Eylül 2021",
"tt-RU": "27 settembre 2021",
"tzm-Latn-DZ": "27 settembre 2021",
"ug-CN": "27 settembre 2021",
"uk-UA": "27 вересня 2021 р.",
"ur-PK": "27 settembre 2021",
"uz-Cyrl-UZ": "27 сентябр, 2021",
"uz-Latn-UZ": "2021 M09 27",
"vi-VN": "27 tháng 9, 2021",
"wo-SN": "27 settembre 2021",
"xh-ZA": "27 settembre 2021",
"yo-NG": "27 settembre 2021",
"zh-CN": "2021年9月27日",
"zh-HK": "2021年9月27日",
"zh-MO": "2021年9月27日",
"zh-SG": "2021年9月27日",
"zh-TW": "2021年9月27日",
"zu-ZA": "27 settembre 2021"
}
The only locales
with the comma after month name are gu-IN
ta-IN
te-IN
and uz-Cyrl-UZ
but none has months in English.