Diff 不是 Moment.js 中的函数 [英] Diff is not a function in Moment.js

查看:34
本文介绍了Diff 不是 Moment.js 中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Air Datepicker 中选择一个日期,并尝试将今天的日期与所选日期进行比较以确定天数差异.因此,例如,如果今天是 12/11/2016 并且我选择 12/20/2016,我想得到差值,即 9.

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.

我一直遇到以下错误:end.diff 不是函数".

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);
     }
 });

小提琴

https://jsfiddle.net/qn530dpq/

推荐答案

onSelect 函数的第一个参数是文本形式的日期 - 而不是矩对象.尝试在表示所选日期的时刻对象上调用 diff 函数,它会起作用.此外,您还必须指定变量end",这可能是您的fd".检查一下:https://jsfiddle.net/t9suf65p/

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);
        }
    });

这篇关于Diff 不是 Moment.js 中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆