Question

I need to parse the datetime attribute to a different div. I don't want the actual string from the time tag as this is formatted but instead want to parse the ISO date

Currently the below code spits out 01 Feb 12 but I need it to be 2012-02-01T00:00:00Z

Is it possible to do this?

var p = $(this);
p.attr("data-date", p.find("span.openDate time").text());

<span class="openDate"><time datetime="2012-02-01T00:00:00Z">01 Feb 12</time><span>

Thanks

Was it helpful?

Solution

Replace you code with this:

var p = $(this);
p.attr("data-date", p.find("span.openDate time").attr("datetime"));

It will now take the content from the datetime attribute

datetime="2012-02-01T00:00:00Z"

So the data-date would be

2012-02-01T00:00:00Z

OTHER TIPS

Try like this:

p.attr("data-date", p.find("span.openDate time").attr('date-time'));
                                                  ^^^
                                                  Change here  

Demo

Use

var dt=$('span.opentime time').attr('datetime');
$(this).attr("data-date",dt);
var x = $('time').attr('datetime');

or

var x = $('span time:first-child').attr('datetime');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top