locked
Ajax Calander Controler date difference RRS feed

  • Question

  • User-2024981698 posted

    Sir,

    I have two AJAX Calander Controls on my form and I want to calculate difference of days between these to controls

          <asp:TextBox ID="txtstdate" runat="server" Height="22px" Width="109px"></asp:TextBox>
                            <asp:CalendarExtender ID="txtstdate_CalendarExtender" runat="server" 
                                TargetControlID="txtstdate">
                            </asp:CalendarExtender>
    
       <asp:TextBox ID="txtenddate" runat="server" Height="22px" Width="110px"  onblur="btndadays_onclick()"></asp:TextBox>
                            <asp:CalendarExtender ID="txtenddate_CalendarExtender" runat="server" 
                                Enabled="True" TargetControlID="txtenddate">
                            </asp:CalendarExtender>
                    

    I have written a Javascript on Blur of second date.

     <script language="javascript" type="text/javascript">
            function btndadays_onclick() {
               
                var st1 = document.getElementById("<%= txtstdate.ClientId %>").value;
                var ed1 = document.getElementById("<%= txtenddate.ClientId %>").value;
    
                var timeDiff = Math.abs(ed1.getTime() - st1.getTime());
                var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
                document.getElementById("<%= txtdays.ClientId %>").value= diffdays ;
             
            }
    
          </script>

    But I am having error as

    TypeError: ed1.getTime is not a function,   Pl. guide
    Math.abs(ed1.getTime())
    Friday, September 9, 2016 4:18 AM

Answers

  • User-1496088595 posted
    .getTime() is function which is available only on date type in javascript and you are using it on string that is why you are getting error

    the solution for this, you need to convert your date string to actual date type then you can use the function to get time ".getTime()".
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 9, 2016 5:19 AM

All replies

  • User-1496088595 posted
    .getTime() is function which is available only on date type in javascript and you are using it on string that is why you are getting error

    the solution for this, you need to convert your date string to actual date type then you can use the function to get time ".getTime()".
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 9, 2016 5:19 AM
  • User-1142886626 posted

    Hi bhushan98,

    TypeError: ed1.getTime is not a function

    The getTime () is a method of JavaScript date object. So you should create new Date () before use it.

    Such as:

    var d = new Date();
    var n = d.getTime();
    

    I have two AJAX Calander Controls on my form and I want to calculate difference of days between these to controls

    You could use JavaScript String split () Method. It could Split a string into an array substring.

    Code below for your reference:

    var d = ed1.split("/")[0] - st1.split("/")[0];
            var m = ed1.split("/")[1] - st1.split("/")[0];
            var y = ed1.split("/")[2] - st1.split("/")[0];
            alert(d + "/" + m + "/" + y);
    

    Best Regards,

    Ailleen

    Monday, September 12, 2016 10:35 AM