-2

I have this td:

<td>Jul 18 2019 12:00AM</td>

and I need to convert it to ISO 8601 format using JavaScript. Vanilla or using jQuery would be fine, it's just that I keep getting an 'undefined' when I attempt to parse it, but I think it is having trouble because of the html tags. Any help would be appreciated, thank you.

 $(function () {
  $("#_ctl0_phMainContent_dgrdClasses3 tbody tr td:nth-child(7)").each(function (index, dateElem) {
    var $dateElem = $(dateElem);
    var formatted = moment($dateElem.text(), 'MMM DD YYYY h:mm:a').format('MMM DD YYYY h:mm:a').toISOString(); 
    $dateElem.text(formatted);   })
 })
  • 2
    Are all dates on your table of this format? You will need to do some string manipulation in order to parse it as a Date Object – wentjun May 13 '19 at 19:19
  • ```var classydate = document.querySelector("#_ctl0_phMainContent_dgrdClasses3 > tbody > tr:nth-child(3) > td:nth-child(7)").innerText; moment(classydate,'MMM DD YYYY h:mm:a').toISOString(); – Notary Classes May 20 '19 at 18:42

1 Answers1

0

You need to add moment to parse it to the custom date string format and then convert it to iso format For more details regarding isoString

moment('Jul 18 2019 12:00AM','MMM DD YYYY h:mm:a').toISOString() 
Murtaza Hussain
  • 3,851
  • 24
  • 30
  • ``` var classydate = document.querySelector("#_ctl0_phMainContent_dgrdClasses3 > tbody > tr:nth-child(3) > td:nth-child(7)").innerText; ``` Produces the format I need. How can I iterate through an html table that looks like this: ``` Mar 30 2019 12:00AM Mar 30 2019 12:00AM ``` ? – Notary Classes May 20 '19 at 22:01