Ireally Rock
Ireally Rock

Reputation: 236

Use the datetime string from the time tag in jQuery

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

Upvotes: 0

Views: 1025

Answers (4)

ozandlb
ozandlb

Reputation: 1024

var x = $('time').attr('datetime');

or

var x = $('span time:first-child').attr('datetime');

Upvotes: 1

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

Try like this:

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

Demo

Upvotes: 1

Bharath R
Bharath R

Reputation: 1531

Use

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

Upvotes: 0

Johnny000
Johnny000

Reputation: 2104

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

Upvotes: 2

Related Questions