Reputation: 43
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
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