如何用jQuery ui datepicker计算今天的天数? [英] How can I calculate the number of days from today with jQuery ui datepicker?

查看:147
本文介绍了如何用jQuery ui datepicker计算今天的天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,



从jQuery datepicker选择日期后,我计算从现在开始的天数到选定日期的时间有点麻烦。 >

我认为datepicker正在更改选择日期的日期格式,导致计算错误,但是我无法弄清楚如何应对这一问题。我确定这是我,但是我看不到树的森林,所以任何建议都是非常有用的。



这是代码:

 < input type =textclass =datepicker-calc/> 
< input type =textid =tbAddDays/>



  $(document) ready(function(){
$(。datepicker-calc)。datepicker({
dateFormat:dd / mm / yy,
changeMonth:true,
changeYear :true,
onSelect:function(date,e){
var expDate = new Date(date);
var date = e.selectedDay;
if(date< 10 ){
date =(0+ e.selectedDay)
}
var month = e.selectedMonth;
if(month< 10){
month =(0+ e.selectedMonth)
}
var year = e.selectedYear;
var endDate = month +/+ date +/+ year;
updateDays(endDate,e);
$(。datepicker-calc)。datepicker(hide);
}
});

函数treatAsUTC(date){
var result = new Date(date);
result.setMinut es(result.getMinutes() - result.getTimezoneOffset());
返回结果;
}

函数updateDays(expDate,e){
var now = new Date();
var startDate = now.toDateString('dd / MM / yyyy');
var exp = new Date(expDate);
var endDate = exp.toDateString('dd / MM / yyyy');
var millisecondsPerDay = 24 * 60 * 60 * 1000;
var totalDays =(treatAsUTC(endDate) - treatAsUTC(startDate))/ millisecondsPerDay;
$('#tbAddDays')。val(totalDays);
}
});


解决方案

意识到我需要显示在jQuery dd / mm / yy(dd / mm / yyyy)格式,所以保留,并对expDate进行了一些操作传递给updateDays()方法,以确保要计算的日期将被JS正确处理。



工作脚本:

  $(。datepicker-calc)。datepicker({ 
dateFormat:dd / mm / yy,
changeMonth:true,
changeYear:true,
onSelect:function(selectdate,e){
var newDate = selectdate.slice(0,2);
var newMonth = selectdate.slice(4,6);
if(newMonth<10){newMonth =(0+ newMonth.slice 0,1));};
var newYear = selectdate.slice(6,11);
var expDate = newMonth +/+ newDate +/+ newYear;
updateDays (expDate,e);
$(。datepicker-calc)。datepicker(hide);
}
});

函数treatAsUTC(date){
var result = new Date(date);
result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
返回结果;
}

函数updateDays(expDate,e){
var now = new Date();
var startDate = now.toDateString('dd / MM / yyyy');
var expire = new Date(expDate);
var endDate = expire.toDateString('dd / MM / yyyy');
var millisecondsPerDay = 24 * 60 * 60 * 1000;
var totalDays =(treatAsUTC(endDate) - treatAsUTC(startDate))/ millisecondsPerDay;
$('#tbAddDays')。val(totalDays);
}


Good day,

I'm having a little trouble with calculating number of days from now to a selected date after selecting a date from jQuery datepicker.

I think that the datepicker is changing the date formatting of the date on selection and causing the calculation to go wrong but I can't quite figure out how to counter this. I am sure it is me but I can't see the forest for the trees so any suggestions would be really helpful.

Here is the code:

<input type="text" class="datepicker-calc" />
<input type="text" id="tbAddDays" />

$(document).ready(function () {
    $(".datepicker-calc").datepicker({
        dateFormat: "dd/mm/yy",
        changeMonth: true,
        changeYear: true,
        onSelect: function (date, e) {
            var expDate = new Date(date);
            var date = e.selectedDay;
            if (date < 10) {
                date = ("0" + e.selectedDay)
            }
            var month = e.selectedMonth;
            if (month < 10) {
                month = ("0" + e.selectedMonth)
            }
            var year = e.selectedYear;
            var endDate = month + "/" + date + "/" + year;
            updateDays(endDate, e);
            $(".datepicker-calc").datepicker("hide");
        }
    });

    function treatAsUTC(date) {
        var result = new Date(date);
        result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
        return result;
    }

    function updateDays(expDate, e) {
        var now = new Date();
        var startDate = now.toDateString('dd/MM/yyyy');
        var exp = new Date(expDate);
        var endDate = exp.toDateString('dd/MM/yyyy');
        var millisecondsPerDay = 24 * 60 * 60 * 1000;
        var totalDays = (treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay;
        $('#tbAddDays').val(totalDays);
    }
});

解决方案

Realised that I need the display to be in jQuery dd/mm/yy (dd/mm/yyyy) format so kept that and did some manipulation to the expDate being passed to the updateDays() method to ensure that the date to be calculated would be handled correctly by JS.

Working script:

$(".datepicker-calc").datepicker({
      dateFormat: "dd/mm/yy",
      changeMonth: true,
      changeYear: true,
      onSelect: function (selectdate, e) {
      var newDate = selectdate.slice(0,2);
      var newMonth = selectdate.slice(4,6);
      if (newMonth < "10") { newMonth = ("0" + newMonth.slice(0,1));};
      var newYear = selectdate.slice(6,11);
      var expDate = newMonth + "/" + newDate + "/" + newYear;
      updateDays(expDate, e);
      $(".datepicker-calc").datepicker("hide");
   }
});

function treatAsUTC(date) {
    var result = new Date(date);
    result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
    return result;
}

function updateDays(expDate, e) {
    var now = new Date();
    var startDate = now.toDateString('dd/MM/yyyy');
    var expire = new Date(expDate);
    var endDate = expire.toDateString('dd/MM/yyyy');
    var millisecondsPerDay = 24 * 60 * 60 * 1000;
    var totalDays = (treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay;
    $('#tbAddDays').val(totalDays);
}

这篇关于如何用jQuery ui datepicker计算今天的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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