Nicolas Antoine
Nicolas Antoine

Reputation: 43

Apply Moment.js to the date in my c # loop

I want to Apply Moment.js to the date in my c# loop, but the function only uses the first value of ".pu_date" to change the other values :

C#:

@foreach( var u in Model.Users)
{
<span class="pu_date"> @u.Date</span> @* '15/08/2017 09:00:00' *@
}

JS:

$('.pu_date').text(moment($('.pu_date').text(), "DD/MM/YYYY hh:mm:ss").fromNow());

Result:

7 days
7 days
7 days

Instead of :

7 days
16 days
20 days

Upvotes: 1

Views: 181

Answers (1)

Menelaos Vergis
Menelaos Vergis

Reputation: 3955

You have to get the date from each element

$( ".pu_date" ).each(function( index ) {
    $( this ).text( moment($( this ).text(), "DD/MM/YYYY hh:mm:ss").fromNow());
});

Upvotes: 2

Related Questions