Skip to content

Instantly share code, notes, and snippets.

@dominikbulaj
Created June 11, 2018 11:36
Show Gist options
  • Save dominikbulaj/27bcb71acb90f31acfa3a5c8e774a717 to your computer and use it in GitHub Desktop.
Save dominikbulaj/27bcb71acb90f31acfa3a5c8e774a717 to your computer and use it in GitHub Desktop.
MySQL DATETIME to JavaScript Date
// example MySQL DATETIME
const dateTime = '2017-02-04 11:23:54';
let dateTimeParts= dateTime.split(/[- :]/); // regular expression split that creates array with: year, month, day, hour, minutes, seconds values
dateTimeParts[1]--; // monthIndex begins with 0 for January and ends with 11 for December so we need to decrement by one
const dateObject = new Date(...dateTimeParts); // our Date object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment