I do have that script:
function() {
var publishedDate = "";
var yoastInfo = JSON.parse(document.head.querySelector('.yoast-schema-graph.yoast-schema-graph--main').innerHTML);
vars = yoastInfo["@graph"];
for(var i in vars) {
if(vars[i]['@type'] === 'WebPage') {
pubDateRaw=(vars[i]['datePublished']);
break;
}
}
publishedDate.concat(pubDateRaw.substring(0,4),pubDateRaw.substring(5,7),pubDateRaw.substring(8,10));
return publishedDate;
}
When I run the code, it gives me a ""
instead of the date.
here is the json:
{
"@context": "https://schema.org",
"@graph": [
{
"@type": "WebPage",
"@id": "id234234",
"url": "https://www.whatevver.com",
"inLanguage": "de-DE",
"name": "this is the name",
"isPartOf": {
"@id": "id234234
},
"primaryImageOfPage": {
"@id": "id234234"
},
"datePublished": "2020-01-14T07:46:12+00:00",
"dateModified": "2020-01-15T08:04:50+00:00",
"description": "description "
},
]
}
So i need to extract the datePubished from that json. What is wrong with my code?
Edit
With the help of you guys, I made this script:
function() {
var publishedDate = "";
var yoastInfo = JSON.parse(document.head.querySelector('.yoast-schema-graph.yoast-schema-graph--main').innerHTML);
vars = yoastInfo["@graph"];
for(var i in vars) {
if(vars[i]['@type'] === 'WebPage') {
publishedDate=(vars[i]['datePublished']);
}
}
return publishedDate;
}
now I need to cut the dateTime 2020-01-14T07:46:12+00:00
to 2020-01-14
what would be the best solution?
EDIT2
Found the Solution at: To the Solution