I am trying to change the format of the date using below React function below.
The date that I am passing is 2021-12-31
My function is like below
export const getDate = (date) => {
if (date?.length > 0) {
moment(date);
}
return moment(date).format('mm/dd/yyyy');
};
Two issues:
Control never goes inside if, even though the length of the date is > 0.
When I print
return moment(date).format('mm/dd/yyyy');it gives value as00/Fr/2021
You are using the wrong string to format. Like explained in the comments,
mmis for minutes enddis for day in a word. You should useMMinstead ofmmto get the month andDDinstead ofddto get the day as a number.Here is the documentation where you can find this: https://momentjs.com/docs/#/parsing/string-format/
In the future it's good to read documentation thoroughly when you get stuck ;-)