两个日期之间相差发现通过javascript从采摘的日期选择器 [英] finding difference between two dates picked from datepicker via javascript

查看:197
本文介绍了两个日期之间相差发现通过javascript从采摘的日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框接通过jQuery日期选择日期。
我想访问他们的JavaScript并找到天数计它们之间的区别。

我经由clientID的访问日期的文本框和单纯服用的差,但它不工作为me..any suggestions..Is那里经由日期选择器以及任何特殊方法填充文本框访问日期值的一些特定的方式发现在天方面的差异?

我的日期字段:

 < TD风格=WIDTH:15%>
                < ASP:文本框ID =txtStartDt=服务器启用=真WIDTH =80%的ValidationGroup =保存>< / ASP:文本框>
                < IMG ALT =选择日期SRC =../图像/节目-calendar.gifBORDER =0的风格=WIDTH:知识+;
                    高度:16px的;的onclick =JavaScript的:calendarPicker('ContentPlaceHolder1_txtStartDt')
                    ID =IMG1/>
            < / TD>
            < TD风格=WIDTH:9%>
                < ASP:标签ID =LABEL3=服务器的CssClass =标签>终止日期:< / ASP:标签>
            < / TD>
            < TD风格=WIDTH:248px>
                < ASP:文本框ID =txtEndDt=服务器启用=真WIDTH =126px>< / ASP:文本框>
                < IMG ALT =选择日期SRC =../图像/节目-calendar.gifBORDER =0的风格=WIDTH:知识+;
                    高度:16px的;的onclick =JavaScript的:calendarPicker('ContentPlaceHolder1_txtEndDt')
                    ID =IMG2/>
            < / TD>              我的javascript:
                        功能CheckDuration(){
            //警报();            VAR toDate1 =的document.getElementById('<%= txtStartDt.ClientID%GT;');
            VAR toDate2 =新的日期(toDate1.value.replace(' - ',''));            变种TODATE = toDate2.setDate(toDate2.getDate());            VAR toDate4 =的document.getElementById('<%= txtEndDt.ClientID%GT;');
            VAR toDate5 =新的日期(toDate1.value.replace(' - ',''));            变种toDate6 = toDate2.setDate(toDate2.getDate());            如果((toDate6 - TODATE)→30)
                确认(选择的时间段是1个多月的持续时间);
        }


解决方案

有内置​​的方法来获取从具有日期选择器输入一个日期,这个日期将是一个javascript日期对象,可以在其上使用的功能像的getTime()得到划时代毫秒,然后只减去从另一个:

  VAR FROM_DATE = $(#from_input)。日期选择器(GETDATE),
    TO_DATE = $(#to_input」)日期选择器('GETDATE')。变种diff_in_milliseconds = to_date.getTime() - from_date.getTime();

现在你必须找出多少毫秒有一天?

编辑:

我要补充一个例子,这个

FIDDLE

I have two text boxes that pick dates via Jquery datepicker. I want to access them in javascript and find difference between them in terms of days.

I am accessing the dates via clientID of the text boxes and simply taking the difference but it is not working for me..any suggestions..Is there some specific way of accessing date values from textboxes filled via datepicker and any special method of finding the difference in terms of days?

my date fields:

                      <td style="width: 15%">
                <asp:TextBox ID="txtStartDt" runat="server" Enabled="true" Width="80%" ValidationGroup="Save"></asp:TextBox>
                <img alt="Select Date" src="../Images/show-calendar.gif" border="0" style="width: 17px;
                    height: 16px;" onclick="javascript:calendarPicker('ContentPlaceHolder1_txtStartDt')"
                    id="IMG1" />
            </td>
            <td style="width: 9%">
                <asp:Label ID="Label3" runat="server" CssClass="label">End Date:</asp:Label>
            </td>
            <td style="width: 248px">
                <asp:TextBox ID="txtEndDt" runat="server" Enabled="true" Width="126px"></asp:TextBox>
                <img alt="Select Date" src="../Images/show-calendar.gif" border="0" style="width: 17px;
                    height: 16px;" onclick="javascript:calendarPicker('ContentPlaceHolder1_txtEndDt')"
                    id="IMG2" />
            </td>







              my javascript:
                        function CheckDuration() {
            //                alert("");

            var toDate1 = document.getElementById('<% =txtStartDt.ClientID%>');
            var toDate2 = new Date(toDate1.value.replace('-', ' '));

            var toDate = toDate2.setDate(toDate2.getDate());

            var toDate4 = document.getElementById('<% =txtEndDt.ClientID%>');
            var toDate5 = new Date(toDate1.value.replace('-', ' '));

            var toDate6 = toDate2.setDate(toDate2.getDate());

            if ((toDate6 - toDate) > 30)
                confirm("Selected time period is of more than 1 month duration");


        }

解决方案

There are built in methods to get a date from an input that has a datepicker, and that date will be a javascript date object, on which you can use functions like getTime() to get milliseconds from epoch, and then just subtract one from the other:

var from_date = $("#from_input").datepicker('getDate'),
    to_date   = $("#to_input").datepicker('getDate');

var diff_in_milliseconds = to_date.getTime() - from_date.getTime();

Now you'll have to figure out how many milliseconds there are in a day?

EDIT:

I'll add an example to this

FIDDLE

这篇关于两个日期之间相差发现通过javascript从采摘的日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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