문제

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

도움이 되었습니까?

해결책

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

다른 팁

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');
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top