sd_dracula sd_dracula - 7 months ago 35
Javascript Question

Convert Javascript date format

I have a textbox with a date in the format of: 2016.March.23

On the same page I need to pull this date into a javascript Date variable but when I try something like

var date = new Date("2016.March.23");
I get invalid date format.

How can I convert this date format to a JS date variable?


Date constructor takes one of these formats only.

var today = new Date();
var birthday = new Date('December 17, 1995 03:24:00');
var birthday = new Date('1995-12-17T03:24:00');
var birthday = new Date(1995, 11, 17);
var birthday = new Date(1995, 11, 17, 3, 24, 0);

You need parse this date using this method.

console.log( parseDate("2016.March.23" ) );
function parseDate(str)
  var monthArr = ["January","Febuary","March","April","May","June","July","August","September","October","November","December"];
  var dateItems = str.split(".");
  return new Date( dateItems[0], monthArr.indexOf(dateItems[1]), dateItems[2]  );