I am getting a date in some format ('yyyymmdd' or 'yyyymm') and need to display it as a string in my current locale.
For this example, the locale is 'en-GB'.
When I get the full format(yyyymmdd), I am presetting dd/mm/yyyy, and if it is partial(yyyymm) I need to display mm/yyyy, but I get 01/mm/yyyy, how can I solve it?
this is my function-
getDateStrFromEsploro(date: string){
if (date.length === 8){
const year = date.substring(0,4);
const month = date.substring(4,6);
const day = date.substring(6);
return DateUtils.getDateAsString(new Date(parseInt(year), parseInt(month)-1, parseInt(day)));
} else if (date.length === 6){
const year = date.substring(0,4);
const month = date.substring(4,6);
return DateUtils.getDateAsString(new Date(parseInt(year), parseInt(month)-1));
}
}
My DateUtils-
public static getDateAsString(date: Date) {
return date.toLocaleDateString();
}