Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Days/Weeks ago from specific date with Moment.js

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.

like image 868
Max Di Campo Avatar asked Aug 01 '18 21:08

Max Di Campo


People also ask

How do you subtract days using moments?

MomentJS - Subtract Method //chaining subtract method var changeddate = moment(). subtract(5, 'days'). subtract(2, 'months'); // subtract object method var changeddate1 = moment(). subtract({ days: 5, months: 2 }); //using duration in subract method var duration = moment.

How do you compare dates with moments?

Compare two dates using Moment.js has a diff() method, which gives the difference between two dates in years, months, days, hours, minutes, seconds, milliseconds, etc. We can use the second unit in our case to find the difference between the two dates. Before using the Moment. js methods, make sure to include Moment.

How do I find moments from a previous date?

Just like this: moment(). subtract(1, 'days') . It will give you the previous day with the same exact current time that is on your local pc. Save this answer.

Is MomentJS deprecated?

MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week.


2 Answers

You can do this with momentjs diff() method, which can return the difference between two dates in days, weeks, months, hours, minutes, ... based on the option you pass to it.

This is how should be your code:

now = moment()
days = now.diff(date, "days")
weeks = now.diff(date, "weeks")

Demo:

$(document).ready(function() {
  $('.timestamp').html((index, html) => {

    let date = moment(html, "DD.MM.YYYY HH:mm", true),
      now = moment(),
      days = now.diff(date, "days"),
      weeks = now.diff(date, "weeks"),
      result = "";

    if (weeks) {
      result += weeks + (weeks === 1 ? " week " : " weeks ");
      days = days % 7;
    } else if (days || weeks === 0) {
      result += days + (days === 1 ? " day" : " days");
    }

    result += '<br />';
    return result;
  });
});
<span class="timestamp">30.07.2018 00:00</span>
<span class="timestamp">12.06.2018 00:00</span>
<span class="timestamp">10.05.2018 00:00</span>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
like image 120
cнŝdk Avatar answered Oct 23 '22 18:10

cнŝdk


Moment.js has fromNow() function that returns "x days" or "x hours ago" from current date/time.

moment([2007, 0, 29]).fromNow();     // 4 years ago
moment([2007, 0, 29]).fromNow(true); // 4 years
like image 12
Nitin Jadhav Avatar answered Oct 23 '22 18:10

Nitin Jadhav