JimyPP
JimyPP

Reputation: 105

Call javascript function multiple times

So, I'm having some troubles finding a way to solve this problem. I know that it seems silly, but I'm really stuck on this.

I have something like:

<div class="tdate">
    Sat Oct 11 01:11:01 +0000 2014
</div>
<div class="tdate">
    Sat Oct 11 01:10:44 +0000 2014
</div>
<div class="tdate">
    Sat Oct 11 00:51:03 +0000 2014
</div>

And this javascript function:

function parseTwitterDate(tdate) {
    var system_date = new Date(Date.parse(tdate));
    var user_date = new Date();
    if (K.ie) {
        system_date = Date.parse(tdate.replace(/( \+)/, ' UTC$1'))
    }
    var diff = Math.floor((user_date - system_date) / 1000);
    if (diff <= 1) {return "just now";}
    if (diff < 20) {return diff + " seconds ago";}
    if (diff < 40) {return "half a minute ago";}
    if (diff < 60) {return "less than a minute ago";}
    if (diff <= 90) {return "one minute ago";}
    if (diff <= 3540) {return Math.round(diff / 60) + " minutes ago";}
    if (diff <= 5400) {return "1 hour ago";}
    if (diff <= 86400) {return Math.round(diff / 3600) + " hours ago";}
    if (diff <= 129600) {return "1 day ago";}
    if (diff < 604800) {return Math.round(diff / 86400) + " days ago";}
    if (diff <= 777600) {return "1 week ago";}
    return "on " + system_date;
}
var K = function () {
    var a = navigator.userAgent;
    return {
        ie: a.match(/MSIE\s([^;]*)/)
    }
}();

What I'm trying to do is to change all the .tdate to a new format (which is what my javascript does). But I dont know how to call this function to change all the different .tdate. If I call parseTwitterDate("Sat Oct 11 00:51:03 +0000 2014"), it will work, but only one time.

Basically, I want to see all the date on my page with this format: "Sat Oct 11 01:11:01 +0000 2014" change to: "47 minutes ago" (or whatever the output is). But I dont know how to call the function parseTwitterDate(tdate).

Sorry for my really bad explanation. Let me know if I don't make myself clear enough.

And thank you very much to anyone who can help me.

Thanks a lot! :)

Upvotes: 0

Views: 97

Answers (3)

honzahommer
honzahommer

Reputation: 839

$('.tdate').each(function() {
    $(this).text(parseTwitterDate($(this).text()));
});

Upvotes: 0

mildog8
mildog8

Reputation: 2154

Wadup! add this in your javascript file.

...

[].forEach.call(document.getElementsByClassName("tdate"), function(tdate) {
  tdate.innerHTML = parseTwitterDate(tdate.innerHTML);
});

Upvotes: 0

blurfus
blurfus

Reputation: 14031

Since you are using JQuery (or at least the JQuery tag is used), you can do this

$(function () {
    $('.tdate').each(function (index, value) {
        $(value).html(parseTwitterDate(($(value).html())))
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tdate">
  Sat Oct 11 01:11:01 +0000 2014
</div>
<div class="tdate">
  Sat Oct 11 01:10:44 +0000 2014
</div>
<div class="tdate">
  Sat Oct 11 00:51:03 +0000 2014
</div>

Upvotes: 1

Related Questions