I am selecting a date in the Air Datepicker and trying to compare today's date to the selected date to determine the difference in days. So, for example, if today is 12/11/2016 and I select 12/20/2016, I want to get the difference, which is 9.
I keep running into the following error: "end.diff is not a function".
I've stripped the following code down to the essentials:
HTML
<form>
    <input id="datereq" name="datereq" type="text" class="dateReq" value="" />
</form>
<div id="selected"></div>
JQUERY
 var date = new Date(),
     disabledDays = [0, 6];
 $('.dateReq').datepicker({
     dateFormat: 'mm/dd/yyyy',
     minDate: new Date(),
     language: 'en',
     autoClose: true,
     onRenderCell: function(date, cellType) {
         if (cellType == 'day') {
             var day = date.getDay(),
                 isDisabled = disabledDays.indexOf(day) != -1;
             return {
                 disabled: isDisabled
             };
         }
     },
     // Display Appropriate Order Type Options
     onSelect: function onSelect(fd, date) {
         var now = moment(new Date()).format('MM/DD/YYYY'),
             end = fd,
             days = end.diff(now, 'days');
         $('#selected').html('now:' + now + 'end:' + end + 'diff:' + days);
         //console.log('end:' + end);
         //console.log('diff:' + days);
     }
 });
Fiddle
https://jsfiddle.net/qn530dpq/
diff() function is used to get the difference in milliseconds of given dates which are passed as parameter to this function. Syntax: moment(). diff(Moment|String|Number|Date|Array, String, Boolean);
To get the difference in milliseconds, use moment#diff like you would use moment#from . To get the difference in another unit of measurement, pass that measurement as the second argument. To get the duration of a difference between two moments, you can pass diff as an argument into moment#duration .
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. With that I began a journey during a Hackathon to replace moment in a core library at my company.
Moment JS allows displaying of date as per localization and in human readable format. You can use MomentJS inside a browser using the script method. It is also available with Node. js and can be installed using npm.
The diff function is only available on moment objects.
Try this instead:
var now = moment(new Date()),
end = moment(fd),
days = end.diff(now, 'days');
                        the first argument of the onSelect function is the date as text - not a moment object. Try to call the diff function on a moment object representing the selected date and it will work. Also you'll have to specify the variable "end" which is probably your "fd". Check it out: https://jsfiddle.net/t9suf65p/
    // Initialize Datepicker
    var date = new Date(),
    disabledDays = [0, 6];
    $('.dateReq').datepicker({
        dateFormat: 'mm/dd/yyyy',
                minDate: new Date(),
        language: 'en',
        autoClose: true,
        onRenderCell: function (date, cellType) {
            if (cellType == 'day') {
                var day = date.getDay(),
                    isDisabled = disabledDays.indexOf(day) != -1;
                return {
                    disabled: isDisabled
                };
            }
        },
        // Display Appropriate Order Type Options
       onSelect: function onSelect(fd, date) {
           var selectedDate = moment(fd, 'MM/DD/YYYY');
           var now = moment(new Date());
           var days = selectedDate.diff(now, 'days');
                $('#selected').html('now: ' + now.format('MM/DD/YYYY') +  'end: ' + selectedDate.format('MM/DD/YYYY') + ' diff: ' + days);
                //console.log('end:' + end);
                //console.log('diff:' + days);
        }
    });
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With