Reformatting a date from YYYY-MM-DD to MM/DD/YYYY 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;
}
Simpler approach

Another option is to construct a Date object (this can be achieved by appending a time at midnight to the date) and pass that to Intl.DateTimeFormat.prototype.format(). Then there is no need to parse it and reconstruct it yourself. 



function formatCourseDate(date) {
  const dateObj = new Date(date + 'T00:00:00');
  return new Intl.DateTimeFormat('en-US').format(dateObj);
}
console.log(formatCourseDate('1995-12-17'));




Reviewing your current code

Because this is Code Review and we don't just give alternate approaches, I wanted to also give feedback on the current code.

Given your current approach of splitting the parts of the date:


const _date = date.split('-');
const dateObj = {month: _date[1], day: _date[2], year: _date[0]};



because ecmascript-6 features like const are used, object destructuring and the shorthand property definition notation could also be used to simplify this code:



const date = '2017-11-05';
const [year, month, day] = date.split('-');
const dateObj = {month, day, year};
console.log('dateObj:', dateObj);