Question

I am creating a Todo list app in which a user will enter a date when a task is to be done and a task list which will display a list of tasks. Now what I want is the list to be displayed with the due date so if the date entered in the database is 24-07-2013 and the current date is 23-7-2013 it will display Due tomorrow. I have tried to use time-ago.js but didn't find it useful.

Here is the code which I used to create the list with date:

db.transaction(function(tx){
    tx.executeSql(select_taskDetails,[],function(tx,results){
        $(".taskList_view").empty();
        if(results.rows.length>0){
            for(var i=0;i<results.rows.length;i++){
                var storedate=results.rows.item(i).set_date;
                $("#datacomp").text(jQuery.timeago(storedate));
                console.log("Assigned to: "+results.rows.item(i).assigned);
                $(".taskList_view").append("<li class='autolist' data-filtertext='"+results.rows.item(i).taskname+"'onclick='selectname("+results.rows.item(i).id+")'><a href='#'>"+results.rows.item(i).taskname+"</a><p>"+results.rows.item(i).description+"</p><span id='datacomp'></span></li>");
                temp_id=results.rows.item(i).id;
                dataobj[temp_id]=results.rows.item(i).description;
            }
            $(".taskList_view").listview('refresh');
            $('#demo_page').trigger('create');
            tap_event();
        }
        else{
            $('#demo-page').append("<h1 style='text-align:center;font-size:10px;margin-top:50px;'>No user data found<br>Please Add user <h1><a href='add_Task.html' style='text-align:center;font-size:8px;'>AddTask</a>");
        }
    });
});

Any suggestion will be helpful. Thanks in advance.

Was it helpful?

Solution

I would suggest using moment.js to accomplish this -

if(results.rows.length > 0){
    for (var i=0; i < results.rows.length; i++){
        var storeDate = moment(results.rows.item(i).set_date, 'MM-DD-YYYY');
        var currentDate = moment().format('MM-DD-YYYY')
        var diff = storeDate.diff(currentDate, 'days'); 
    }
}

Here are the moment.js docs on displaying difference

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top