0

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! :)

3
  • why are you not using IDs ? Commented Oct 11, 2014 at 2:11
  • @Meer Because I have more that one DIV with the same name, and same IDs can be used only one time. But why are you asking that? Commented Oct 11, 2014 at 2:14
  • Don't you know how to write a for loop? Commented Oct 11, 2014 at 2:16

3 Answers 3

1

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>

Sign up to request clarification or add additional context in comments.

3 Comments

@JimyPP If you are only planing on including jQuery for doing a simple loop, I would strongly advise against using this.
Agreed, hence the qualifying comment that 'since [OP] was using Jquery' already.
I'm actually already using jQuery. And it works just fine this way. :) Thank you @blurfus
0

Wadup! add this in your javascript file.

...

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

4 Comments

I've just tried, but it doesn't seems to work. Nothing changed. But the solution below worked just fine.
I didn't saw that either. Thank you anyway :)
It works fine now indeed. I think i'm going to use this instead of jQuery, it will be faster. Thank you for your help. @AndrewM
@JimtPP my absolute pleasure!
0
$('.tdate').each(function() {
    $(this).text(parseTwitterDate($(this).text()));
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.