1

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

DEMO

// [[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()]]
});

console.log(arr)

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

5
  • 1
    Where's the year ? Is 33 for 1933 ? 2033 ? 8331 ? Commented Jan 20, 2014 at 13:56
  • For complex date parsing, I'd suggest you to look at moment.js. But I think you ask a little too fast, did you search for "javascript date parsing" ? Commented Jan 20, 2014 at 13:58
  • by default it can be this year 2014 Commented Jan 20, 2014 at 13:58
  • So basically you need to convert [20th Jan, 33] into a date object right? Commented Jan 20, 2014 at 14:05
  • AJ that is correct, 20th Jan 2013 into a date object. So basically all string dates within the table and returned within the array. the 33 stays as a string. Commented Jan 20, 2014 at 14:07

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

'Jan,' should return 0 since month are zero based.
0

You can always convert a string to date as

var myDate = new Date("2013/1/16");

You can clearly get the "2013/1/16" from the array provided the months are in numbers.

click here for more info on date object in JavaScript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.