-1

I want to extract the value of datetime from each of the following array objects:

[<time pubdate class="dt-updated" datetime="2015-07-09T11:50:32+0000" title="Time posted: 09 Jul 2015, 11:50:32 (UTC)" aria-label="Posted on 09 Jul">09 Jul</time>, <time pubdate class="dt-updated" datetime="2015-06-18T13:59:49+0000" title="Time posted: 18 Jun 2015, 13:59:49 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>, <time pubdate class="dt-updated" datetime="2015-06-18T12:56:47+0000" title="Time posted: 18 Jun 2015, 12:56:47 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>, <time pubdate class="dt-updated" datetime="2015-06-18T12:41:42+0000" title="Time posted: 18 Jun 2015, 12:41:42 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>]

Does anyone know if theres a method for array objects that helps me do this when I create my loop? My searching online hasn't found anything so far...

theclarkofben
  • 425
  • 2
  • 6
  • 18
  • 1
    use regular expressions to find it. – DLeh Jul 13 '15 at 14:22
  • so those are dom elements? how did you get them? – k-nut Jul 13 '15 at 14:23
  • 1
    As far as JavaScript goes those array elements are illegal. If you want to make that data usable you need to either convert it to strings or some other kind of data that is actually accessible by JavaScript. –  Jul 13 '15 at 14:27
  • Got them from a regex of a JSON I retrieved – theclarkofben Jul 13 '15 at 14:27
  • can you provide the full json? – k-nut Jul 13 '15 at 14:29
  • If you already had all the data in JSON why even convert it to array elements? –  Jul 13 '15 at 14:30
  • http://codebeautify.org/jsonviewer/c20f25 – theclarkofben Jul 13 '15 at 14:31
  • but that is just a string in the json, not an array?! – k-nut Jul 13 '15 at 14:35
  • Do you have an array of HTML strings? It's completely unclear what you have. If you have an array of objects, it's a duplicate of http://stackoverflow.com/q/19590865/218196. – Felix Kling Jul 13 '15 at 14:36
  • OK so regex was performed on the JSON extracting all the – theclarkofben Jul 13 '15 at 14:42
  • 1
    But a regular expression only works on strings. It does not magically create ` – Felix Kling Jul 13 '15 at 14:43
  • http://stackoverflow.com/questions/476105/how-can-i-convert-string-to-datetime-with-format-specification-in-javascript – Azevedo Jul 13 '15 at 14:44
  • You can see the code that extracts the – theclarkofben Jul 13 '15 at 14:58

1 Answers1

1

I'd use a regex match datetime="(.*?)"

https://regex101.com/r/wE0mZ8/1

This will put all your datetime values into an array:

var str = '[<time pubdate class="dt-updated" datetime="2015-07-09T11:50:32+0000" title="Time posted: 09 Jul 2015, 11:50:32 (UTC)" aria-label="Posted on 09 Jul">09 Jul</time>, <time pubdate class="dt-updated" datetime="2015-06-18T13:59:49+0000" title="Time posted: 18 Jun 2015, 13:59:49 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>, <time pubdate class="dt-updated" datetime="2015-06-18T12:56:47+0000" title="Time posted: 18 Jun 2015, 12:56:47 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>, <time pubdate class="dt-updated" datetime="2015-06-18T12:41:42+0000" title="Time posted: 18 Jun 2015, 12:41:42 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>]';
var res = str.match(/datetime="(.*?)"/gi); 
alert (res[0]);
Azevedo
  • 2,059
  • 6
  • 34
  • 52