Question

Im working on an underscore.js template in backbone. In the template i receive to dates in unix time. I convert them to a date by multiply them by 1000.

In a loop im trying to compare them. i Want to check how many days its betweeen them. I want to be able to print different html depending on if the difference between the dates is more or less than 5 days.

So if less than 5 days ago it got uploaded i want to print some html. If not i want to print some other html.

How would i do that? Anyone that know?

<% collection.each(function(model,index) { 

    var a = moment(new Date(model.get("uploaded_date")*1000)); /*represents when the item got uploaded/approved*/
    var b = moment(new Date(now)*1000); /*represents the server time*/
    var diff = b.diff(a, 'days');

    %>

    <div class="content">
        <% if(diff<5) { %>

            /*doesn't work*/

        <% } %>   
    </div>

<% }); %>
Was it helpful?

Solution

Try this :

var b = moment(new Date(now*1000)); /*represents the server time*/

instead of :

var b = moment(new Date(now)*1000); /*represents the server time*/

By default, moment#diff will return number rounded down. If you want the floating point number, pass true as the third argument :

var diff = b.diff(a, 'days', true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top