I work with moment.js and I have 3 different dates, e.g.
- 30.07.2018
- 12.06.2018
- 10.05.2018
I now try to get the difference in days from these dates until today (if it is less then 7 days ago) or the weeks until today (if it more than 7 days ago) and place it in several spans.
UPDATE thanks Thomas!
I got:
$(document).ready(function(){
$('.timestamp').html((index, html) => {
let date = moment(html, "DD.MM.YYYY HH:mm", true),
now = moment(),
days = Math.floor(Math.abs(date - now) / 86400000),
weeks = Math.floor(days/7),
result = date.format("DD.MM.YYYY") + " - ";
if(weeks){
result += weeks + (weeks===1? " week ": " weeks ");
days = days % 7;
}
if(days || weeks===0){
result += days + (days === 1? " day": " days");
}
return result;
});
});
What I still need:
Not showing the initial date, just showing
"3 Days"
. If it delete "result", I want work anymore.Not showing
"7 weeks 2 days"
, this should just be"7 weeks"
Here is the actual fiddle.