Snippets

Daniel Shaw BD: datePublished

Created by Daniel Shaw last modified
<script>
    // Get the date.
    var hasDate = document.querySelector('time');

    if (hasDate) {
        datePublished = hasDate.innerText
        datePublished = datePublished.replace(/(\d+)(st|nd|rd|th)/g, '$1');
        datePublished = new Date(datePublished);

        var date = new Date(datePublished.getTime() - (datePublished.getTimezoneOffset() * 60000 )).toISOString().split("T")[0];

        // Generate schema.
        var schema = {
            "@context": "https://schema.org",
            "@type": "NewsArticle",
            "url": "https://www.fiordlandoutdoors.co.nz/blog/which-is-better-routeburn-track-or-milford-track/",
            "mainEntityOfPage": "https://www.fiordlandoutdoors.co.nz/blog/which-is-better-routeburn-track-or-milford-track/",
            "author": "Fiordland Outdoors Co.",
            "publisher":{
                "@type":"Organization",
                "name":"Fiordland Outdoors Co.",
                "logo": "https://www.fiordlandoutdoors.co.nz/logo.jpg"
            },
            "headline": "Which is better: Routeburn Track or Milford Track?",
            "image": "https://www.fiordlandoutdoors.co.nz/article.jpg",
            "datePublished": date,
            "dateModified": date
        }

        // Inject script to head.
        var script = document.createElement('script');
        script.type = "application/ld+json";
        script.text = JSON.stringify(schema);
        document.querySelector('head').appendChild(script);
    }
</script>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.