Blogger'da "Son Güncelleme" Zamanını Gösterme

Blog yazılarınızın ne zaman güncellendiğini "3 gün önce" veya "2 ay önce" gibi dinamik ve modern bir şekilde göstermek isteyenler için basit bir kod. 

<b:if cond='data:view.isSingleItem  AND data:post.lastUpdated != data:post.date'>
<span class='post-updated' expr:data-updated='data:post.lastUpdated.ISO8601'>
</span>
<script type='text/javascript'>//<![CDATA[
function relativeTime(dateString) {
    const now = new Date();
    const updated = new Date(dateString);
    const diffInSeconds = Math.floor((now - updated) / 1000);
    const intervals = {
        yıl: 31536000,
        ay: 2592000,
        hafta: 604800,
        gün: 86400,
        saat: 3600,
        dakika: 60,
        saniye: 1
    };
    for (let key in intervals) {
        let counter = Math.floor(diffInSeconds / intervals[key]);
        if (counter > 0) {
            return counter + ' ' + key + ' önce';
        }
    }
    return 'Az önce';
}
document.querySelectorAll('.post-updated').forEach(function(el) {
    const rawDate = el.getAttribute('data-updated');
    if (rawDate) {
        el.innerText = 'Güncellendi: ' + relativeTime(rawDate);
    }
});
//]]>
</script>
</b:if>

Kodu temanızın HTML düzenleme kısmında, yazı içeriğinin görünmesini istediğiniz yere yapıştırmanız yeterlidir. 

3 Yorum