简体   繁体   中英

Node.js Scrape datetime from a DOM

I have a simple question.

I have this HTML code

  <time datetime="2014-02-18"> 18 February 2014</time>

How i can scrape the datetime in format "2014-02-18" ?

I tryed

var date = $('time').value;

or

var date = $('time').dateTime;

but both return undefined on console.

Thanks!

(I assume you're using jQuery in Node or cheerio or similar.)

You can get the string via attr :

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

Then convert it to a date in your favorite way, being sure to allow for the right timezone. For instance, you could do

var date = new Date(dateString);

...after first checking what the version of V8 in your version of Node does with that, since unfortunately the specification floundered on it:

  • It wasn't defined at all prior to ES5
  • It was mis-defined in ES5 as being UTC if there's no timezone indicator instead of local time as per ISO-8601, so "2014-02-18" would have been UTC, but:
  • That was fixed in ES2015, so "2014-02-18" would be interpreted as local time, but:
  • And then updated in ES2016 to say that date-only forms should be UTC and date-time forms should be local time, so "2014-02-18" would be UTC again

So just be sure you know what your copy of V8 is doing if you use that, or use new Date(year, month, day) (for local time) or new Date(Date.UTC(year, month day)) (for UTC) to handle it yourself.

time is not a value field so .value will not work.

In javascript you can use ,

document.getElementsByTagName("time")[0].getAttribute("datetime");

And if you are using JQuery you can use,

$('time').attr('datetime')

datetime is basically an attribute of time field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM