Calculate number of days between two dates with momentsjs

16,512

Solution 1

Apart from .diff(), you could also use moment durations: http://momentjs.com/docs/#/durations/

Edit: Don't know what was I thinking back then. Thanks @aedm for pointing it out. There is no need of duration here, which is for creating time spans.

.diff works as intended, just that you have to make sure you are using the correct format to parse the dates.

Example Fiddle: https://jsfiddle.net/abhitalks/md4jte5d/

Example Snippet:

$("#btn").on('click', function(e) {
	var fromDate = $('#fromDate').val(), 
  		toDate = $('#toDate').val(), 
		from, to, druation;
  
	from = moment(fromDate, 'YYYY-MM-DD'); // format in which you have the date
	to = moment(toDate, 'YYYY-MM-DD');     // format in which you have the date
  
	/* using diff */
	duration = to.diff(from, 'days')     
	
	/* show the result */
	$('#result').text(duration + ' days');
  
});
<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.11.1/moment.min.js"></script>
From: <input id="fromDate" type='date' />
To: <input id="toDate" type='date' />&nbsp;&nbsp;
<button id="btn">Submit</button><hr />
<p id="result"></p>

As I see in your code, you have comments like this:

// I can format the "from" var the way I want

No. You cannot format the dates the way you want. You have to use the format in which the date you are parsing is in. In my example, as I am using the HTML5 date, the returned date is in the yyyy-mm-dd format, hence using that format. In your case, determine what format your textboxes return the dates in, and use that format.

You may also omit the format altogether, if you are sure that the returned date is in a standard format.

Solution 2

To get the difference in another unit of measurement, pass that measurement as the second argument.

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1

Refer this for further

Share:
16,512
Marian Rick
Author by

Marian Rick

I'm all about digital brand and product development. Currently accompanying Finanzguru on its way to becoming Europe’s largest independent financial platform.

Updated on June 23, 2022

Comments

  • Marian Rick
    Marian Rick almost 2 years

    I got two dates stored inside a var (created by a date picker). To format these I am using moments.js

    I want to calculate the number of days between these two fields and store this number inside a var days. This is my code so far:

    // When someone clicks my submit button, I create an url stored in the var url.
    $(".book_submit").on('click', function(e){
    
      // I get the datepicker and input fields data
      var from = $('.from').val();
      var to = $('.to').val();
    
      // I can format the "from" var the way I want
      from  = moment(from, "DD.MM.YYYY");
      from = moment(from).format("YYYY-MM-DD");
    
      // I can format the "to" var the way I want
      to = moment(to, "DD.MM.YYYY");
      to = moment(to).format("YYYY-MM-DD");
    
      // I want to store the number between the two dates in the var "days", to place it inside my link below. I have found ".diff(to, 'days') in the moments documentation.
    
      //days = from.diff(to, 'days');   // =1
    
      var url = "https://mycustomlink&days= " + days + "";
    
    });
    

    I can't get from.diff(to, 'days') to work and store it inside a variable. I have tried to set it like:

    var days = from.diff(to, 'days');
    

    How can I store the difference between these to days inside my var days? I appreciate any suggestion.

  • Marian Rick
    Marian Rick about 8 years
    Thanks for your suggestion! I have found this inside the documentation as well, but to be honest I even do not know how to replace the dates displayed in your example with my vars. Can you help me with that?
  • WP Learner
    WP Learner about 8 years
    Here first variable is the year, second is the month and third is the date. So, you need to pass two values relatively.
  • aedm
    aedm almost 8 years
    This answer is wrong. Try your fiddle with "10/01/2000" and "11/02/2000".
  • aedm
    aedm almost 8 years
    To make it clear, duration.days() is wrong, because it only gives you the days part of the difference. However, to.diff(from, 'days') works as intended.
  • Abhitalks
    Abhitalks over 7 years
    Thanks @aedm. Have corrected the answer. Don't know what was I thinking back then :/ Must've been up late night and mixed up things from elsewhere!! Thank you once again, it would have gone unnoticed if it weren't you.
  • aedm
    aedm over 7 years
    Thanks for fixing. :)