我试图让用户使用带有新 HTML5 时间输入和日期输入的 momentjs 来选择他的约会时间和日期。
日期输入的标记
<input data-bind="value: startDate" type="date" ></input>
<output id=startDate data-bind="text: startDate"></output>
<br />
<input data-bind="value: endDate" type="date"></input>
<output id=endDate data-bind="text: endDate"></output>
<pre data-bind="text: ko.toJSON($data, null, 2)"></pre>
JS
//var todayDate = (new Date()).toISOString().split('T')[0],
    viewModel = {
    //reset startDate and EndDate
    startDate: ko.observable(moment(new moment()).format("YYYY-MM-DD")),
    endDate: ko.observable(moment(new moment()).format("YYYY-MM-DD")),
    //set back the endDtate to 2034-04-28
    //endDate: ko.observable("2034-04-28"),
    dateDiff : ko.computed(function () {
      return  moment.duration(this.endDate - this.startDate).humanize();
    })
};
viewModel.startDate.subscribe(function(newValue) {
    // Show the updated value in the console
    console.info(newValue.replace(/\D/g,''));
});
viewModel.endDate.subscribe(function(newValue) {
    // Show the updated value in the console
    console.info(newValue.replace(/\D/g,''));
});
ko.applyBindings(viewModel);
演示:http: //jsfiddle.net/kougiland/pBFQA/2/
我希望当用户更改日期输入时,它会重新计算并返回差异,如果结束日期小于开始日期,它会发出警报并将结束日期设置回 startdate 值。
我想对 Html5 时间选择器做同样的事情
谢谢你
更新:HTML5时间
标记
<input data-bind="value: startTime" type="Time" ></input>
<output id=startTime data-bind="text: startTime"></output>
<br />
<input data-bind="value: endTime" type="Time"></input>
<output id=endTime data-bind="text: endTime"></output>
<pre data-bind="text: ko.toJSON($data, null, 2)"></pre>
JS
    viewModel = {
    //reset startTime and EndTime
    startTime: ko.observable(moment().format('HH:mm:ss')),
    endTime: ko.observable(moment().format('HH:mm:ss')),
    timeDiff : ko.computed(function () {
      console.log(this.startTime);
      console.log(this.endTime);
      return  moment.duration(this.endTime - this.startTime).humanize();
    })
};
ko.applyBindings(viewModel);
演示:http: //jsfiddle.net/kougiland/5Bjzh/1/
如何更新timeDiff输入更改?