Redwall Redwall - 1 year ago 30
Javascript Question

Convert String to Date Object in Array

I have a table which has a date string in the header. It is returned as part of an array but I need that date in the array to be a

Date Object
and not just a string.

My table is as follows


// [[20th Jan, 33], [21st Jan, 44], [22nd Jan, 5],[23rd Jan, 17]]

I use the following JS to get this array

var arr = $.map($('#bookedTable th:not(:first)'), function(el,i) {
return [[$(el).text(), $('#bookedTable td:eq('+i+')').text()]]


Question: How can I return the string dates as date objects in my array?


You need to ran something like this function on all your dates in array :)

function parseThisDate(date) {
  dateParts = date.split(" ");

  return new Date(dateParts[2], translateMonthToNum(dateParts[1]), dateParts[0])

function translateMonthToNum(monthName) {
  if (monthName == 'Jan,') return 0;
  //todo: add all months you need