Formatting the date time with Javascript

You can do a simple string manipulation and create js date object. See function below, which accepts date in format //yyyy-mm-dd hh:mm:ss

DEMO here

function toJSDate (dateTime) {

var dateTime = dateTime.split(" ");//dateTime[0] = date, dateTime[1] = time

var date = dateTime[0].split("-");
var time = dateTime[1].split(":");

//(year, month, day, hours, minutes, seconds, milliseconds)
// mont is 0 indexed so date[1] - 1 corrected format
return new Date(date[0], date[1]-1, date[2], time[0], time[1], time[2], 0);

Leave a Comment