In this small bit of JavaScript, a date is passed in as YYYY-MM-DD
. It's formatted like this to be passed to the backend system. On the front end, we need to display it as MM/DD/YYYY
.
I split the string, and put the each array index in the correct position. However, I feel like there could be a a better way to do this.
This is the code I have at the moment.
static formatCourseDate(date: string): string {
const _date = date.split('-');
const dateObj = {month: _date[1], day: _date[2], year: _date[0]};
return dateObj.month + '/' + dateObj.day + '/' + dateObj.year;
}