从 JavaScript 中的 raddatepicker 获取选定的日期

Posted

技术标签:

【中文标题】从 JavaScript 中的 raddatepicker 获取选定的日期【英文标题】:Get selected date from raddatepicker in javascript 【发布时间】:2012-06-25 11:13:08 【问题描述】:

大家好,我对Rad Panel bar 的所有设计如下

 <telerik:RadPanelBar runat="server" ID="rdpnlPersonal" ExpandMode="FullExpandedItem"
                Skin="Vista" EnableEmbeddedSkins="false" Width="880px">
                <Items>
                    <telerik:RadPanelItem Expanded="true" Text="Employee Personal Information" runat="server"
                        Selected="true">
                        <Items>
                            <telerik:RadPanelItem Value="AccountInformation" runat="server">
                                <ItemTemplate>
                                    <label>
                                        Demop
                                    </label>
                                    <telerik:RadDatePicker ID="RadDatePicker1" Width="200px" runat="server" MinDate="1900-01-01"
                                        TabIndex="5">
                                        <Calendar ID="Calendar1" RangeMinDate="1900-01-01" runat="server">
                                        </Calendar>
                                    </telerik:RadDatePicker>
                                    <telerik:RadDatePicker ID="radDtHiredate" Width="200px" runat="server" MinDate="1900-01-01"
                                        TabIndex="5">
                                        <Calendar ID="calHire" RangeMinDate="1900-01-01" runat="server">
                                        </Calendar>
                                    </telerik:RadDatePicker>
                                    <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="validatehiredate"
                                        SetFocusOnError="true" ControlToValidate="radDtHiredate" ValidationGroup="accountValidation"
                                        Display="Dynamic">Can not Hire Guy less than 18 yrs</asp:CustomValidator>
                                    <br />
                                    <telerik:RadButton ID="rdbtnwhAnother" Text="Add WithHolding" runat="server" ButtonType="LinkButton"
                                        ValidationGroup="accountValidation" OnClick="rdbtnwhAnother_Click">
                                    </telerik:RadButton>
                                </ItemTemplate>
                            </telerik:RadPanelItem>
                        </Items>
                    </telerik:RadPanelItem>
                </Items>
            </telerik:RadPanelBar>

这里我需要的是,如果所选日期小于18 years,我想按照Custom validator 显示错误消息。为此,我编写了以下脚本,但我无法实现所需的..

我的脚本如下

 <script type="text/javascript" language="javascript">
    function monthDiff(d1, d2) 
        alert("Mon");
        var months;
        var date1 = new Date(d1);
        var date2 = new Date(d2);
        return (date1 - date2) / (1000 * 60 * 60 * 24);
        return months;
    


    //function getLeapYear

    function difference(d1, d2) 
        var hiredate, dob; var diff = 18 * 12;
        hiredate = document.getElementById(d1).get_selectedDate();
        dob = document.getElementById(d2).get_selectedDate();
        var Age = monthDiff(hiredate, dob);

        var compareVal = 365 * 18; //getCompareVal(hiredate,dob);

        if (Age >= compareVal) 
            return true;
            //true
         else 
            return false; //false
        
    
    function validatehiredate(value, arg) 
        var datePicker = $find('<%# rdpnlPersonal.FindItemByText("Employee Personal Information").FindControl("RadDatePicker1").ClientID %>');
        var datePicker1 = $find('<%# rdpnlPersonal.FindItemByText("Employee Personal Information").FindControl("radDtDOB").ClientID %>');
        arg.IsValid = (difference(datePicker, datePicker1));
    
</script>

谁能帮帮我

【问题讨论】:

【参考方案1】:

毕竟我通过在radcodeblock中编写脚本如下解决了

<telerik:RadCodeBlock runat="server" ID="radcb">
        <script type="text/javascript" language="javascript">
            function monthDiff(d1, d2) 
                var months;
                var date1 = new Date(d1);
                var date2 = new Date(d2);
                return (date1 - date2) / (1000 * 60 * 60 * 24);
                return months;
            

            //function getLeapYear

            function difference(d1, d2) 
                var diff = 18 * 12;
                var Age = monthDiff(d1, d2);

                var compareVal = 365 * 18; //getCompareVal(hiredate,dob);

                if (Age >= compareVal) 
                    return true;
                    //true
                 else 
                    return false; //false
                
            
            function validatehiredate(value, arg) 
                var datePicker = $find('<%= rdpnlPersonal.FindItemByValue("AccountInformation").FindControl("radDtHiredate").ClientID %>');
                var datePicker1 = $find('<%= rdpnlPersonal.FindItemByValue("AccountInformation").FindControl("radDtDOB").ClientID %>');

                var date = datePicker.get_selectedDate();
                var date1 = datePicker1.get_selectedDate();
                arg.IsValid = (difference(date, date1));
            
        </script>
    </telerik:RadCodeBlock>

【讨论】:

以上是关于从 JavaScript 中的 raddatepicker 获取选定的日期的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 JavaScriptCore 从 Swift 中的 javascript SDK 调用异步 javascript 函数

从 javascript 类中的“私有”方法访问“公共”方法

javascript 从javascript中的多个数组中获取单个数组(合并数组)

JavaScript 从Javascript中的字符串中剥离html标记

从javascript中的点坐标计算体积

如何从 javascript 中的 URL 获取值