jQuery Datepicker日数 [英] jQuery Datepicker day count

查看:98
本文介绍了jQuery Datepicker日数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个jQuery UI日期选择器,当他们都选择了日期时,我希望这些日期之间的区别显示在一个单独的输入[type =text]中



此外,我理想地喜欢我的计数减去任何周末天数,只计算几天



我的相关代码如下:



JS:

  $('#firstday,#lastday')。datepicker({
dateFormat:'dd / mm / yy'
});

XHTML:

 code>< label for =firstday>离开的第一天< / label> 
< input type =textname =firstdayid =firstday/>

< label for =lastday>离开的最后一天< / label>
< input type =textname =lastdayid =lastday/>

< label for =totaldays>总天数< / label>
< input type =textname =totaldaysid =totaldays/>

很多搜索都带给我很多不同的解决方案,我没有一个可以上班我希望任何想法都不会被赞赏。

解决方案

这样的事情应该可以工作:

  $(#firstday,#lastday)。datepicker({
onSelect:function(){

//日期将以毫秒为单位,这就是为什么我们用1000 * 60 * 60 * 24

var firstday = new Date($(#firstday)。val()。split /)。reverse()。join(,));
var lastday = new Date($(#lastday)val()。split(/)。 (,);
$(#totaldays)。val((lastday - firstday)/ 86400000);
}
});

在节点控制台中,它给出:

  > x = new Date(18/5/2010.split(/)。reverse()。join(,))
2010年5月17日星期一22:00:00 GMT
> y = new Date(18/5/2015.split(/)。reverse()。join(,))
Sun,2015年5月17日22:00:00 GMT
> x-y
-157766400000
> y-x
157766400000
> (yx)/ 86400000
1826

- 编辑 -



当您有开始日期和结束日期时,可以使用从星期日0返回的getDay()轻松计算周末天数,星期一为1,星期六为6。 >

Yo可以使用.getMonth()和.getDate()结合少数其他{}条件的假期。

  var weekend_count = 0; (i = firstday.valueOf(); i< = lastday.valueOf(); i + = 86400000)
{
var temp = new Date(i);
if(temp.getDay()== 0 || temp.getDay()== 6){
weekend_count ++;
}
}

最后你只是做

  $(#totaldays)。val((lastday  -  firstday)/ 86400000  -  weekend_count); 

只是在结尾注释。你应该在一个单独的函数中推断这个代码(尽可能多的),以便保持你的代码更容易维护,以及在其他地方需要相同的功能。



祝你好运。


I've got two jQuery UI datepickers and when they've both had dates chosen, I'd like the difference between these dates to be displayed in a separate input[type="text"] as soon as the second date is selected.

Also, I'd ideally like my count to subtract any weekend days and just count days from Monday - Friday.

My (relevant) code is as follows:

JS:

$('#firstday, #lastday').datepicker({
  dateFormat: 'dd/mm/yy'
});

XHTML:

<label for="firstday">First Day of Leave</label>
<input type="text" name="firstday" id="firstday" />

<label for="lastday">Last Day of Leave</label>
<input type="text" name="lastday" id="lastday" />

<label for="totaldays">Total Days</label>
<input type="text" name="totaldays" id="totaldays" />

Lots of searching has led me to lots of different solutions, none of which I can get to work as I'd like so any ideas would be appreciated.

解决方案

Something like this should work:

$("#firstday, #lastday").datepicker({
  onSelect: function (){

    // Date will give time difference in miliseconds, that is why we divide with 1000*60*60*24

    var firstday = new Date($("#firstday").val().split("/").reverse().join(","));
    var lastday = new Date($("#lastday").val().split("/").reverse().join(",");
    $("#totaldays").val((lastday - firstday) / 86400000);        
  }
});

In node console it gives:

> x = new Date("18/5/2010".split("/").reverse().join(","))
Mon, 17 May 2010 22:00:00 GMT
> y = new Date("18/5/2015".split("/").reverse().join(","))
Sun, 17 May 2015 22:00:00 GMT
> x-y
-157766400000
> y-x
157766400000
> (y-x)/86400000
1826

-- EDIT --

When you have starting date and ending date number of days that are weekend is easily calculated using getDay() which returns from 0 for Sunday, 1 for Monday ... 6 for Saturday.

Yo could use .getMonth() and .getDate() combined with few else{} conditions for holidays as well.

var weekend_count = 0;
for (i = firstday.valueOf(); i <= lastday.valueOf(); i+= 86400000){
 var temp = new Date(i);
 if (temp.getDay() == 0 || temp.getDay() == 6) {
   weekend_count++;
 }
}

and in the end you just do

$("#totaldays").val( (lastday - firstday) / 86400000 - weekend_count);

Just to make a note at the end. You should probably extrapolate this code (as much of it as you can) in a separate function in order to keep your code easier to maintain and in case you need that same function on some other place.

Good luck.

这篇关于jQuery Datepicker日数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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