JavaScript: Parsing dates with the destructuring assignment syntax

As I was scraping some stuff in a node module I got dates that were either full dates with time (“2017-01-01 12:00”) or just dates without time (“2017-01-01”) but I wanted to do some checking with months and days before sending back a proper Date-object, so I had to parse it out. Using the new ES6 destructuring assignment syntax it got really good looking:

/**
 * Converts a standard format date string, such as "2017-01-03 12:30" or "2017-05-06"
 */
module.exports = (dateRaw) => {
    if(dateRaw === undefined) {
        throw new Error('Invalid date provided!');
    }

    // Returns ['2017-01-03', '12:30']
    const [date, time] = dateRaw.trim().split(' '); 

    // Returns ['2017', '01', '03']
    const [year, month, day] = date.split('-'); 

    // Returns either the time, or, it it's missing, 0 hours and 0 minutes
    const [hours, minutes] = (time !== undefined) ? time.split(':') : [0, 0]; 

    // Do stuff...

    // To get the timezone correct, use Date.UTC
    return new Date(Date.UTC(year, month - 1, day, hours, minutes));
};

Just thought I’d share, because this used to get slightly ugly in ES5…