I have a date field from backend response where the format changes as we manually change the browser language.
For example if the browser language is french my date from response from backend would be something like this 23 juin 2021
or 30 déc. 2020
. I have to format in UI to MM/DD/YYYY
format.
My code to format it to MM/DD/YYYY
let momentLocale= moment.locale(this.translateService.currentLang)
schedule.startTime.dateText= moment(schedule.startTime.dateText).format("MM/DD/yyyy")
This is not working and its setting value as invalid date to schedule.startTime.dateText
How can i format date to MM/DD/YYYY
where months are in different languages and the language changes when the browser setting language changes?
Is moment.js required or I can do it using Datepipe?
You need to import the moment
locale(s) you're going to use and pass the input format to the moment
parsing function:
import 'moment/locale/fr';
...
let momentLocale = moment.locale(this.translateService.currentLang);
schedule.startTime.dateText= moment(schedule.startTime.dateText, "DD MMM YYYY").format("MM/DD/yyyy");