RadDatePicker c# count days between dates

5 posts, 0 answers
  1. Piseth
    Piseth avatar
    1 posts
    Member since:
    Oct 2012

    Posted 03 Oct 2012 Link to this post

    Hi,

    I have 2 RadDatePickers.

    and 1 button.



    I want to know how to get number of day between selected date 1 and selected date 2 when I click the button.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 Oct 2012 Link to this post

    Hi Piseth,

    Try the following code to achieve your scenario.

    C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
       int Days = RadDatePicker2.SelectedDate.Value.Subtract(RadDatePicker1.SelectedDate.Value).Days;
       Response.Write(Days);
    }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. <span>w</span>
    <span>w</span> avatar
    84 posts
    Member since:
    Jun 2012

    Posted 14 Dec 2012 Link to this post

    Hi Princy
     Is there any way to get day between two dates?
    Let say, i pick date 12/1/2012 - 12/3/2012. when i pick second date, it automaically calculate days diferrence in javascript. such as 3 days.

    Thanks,
    Muhammad Waseem.


  5. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 14 Dec 2012 Link to this post

    Hi Muhammad,

    Try this:

    ASPX:
    <telerik:RadDatePicker runat="server" ID="RadDatePicker1"></telerik:RadDatePicker>
    <telerik:RadDatePicker runat="server" ID="RadDatePicker2" AutoPostBack="true" OnSelectedDateChanged="RadDatePicker2_SelectedDateChanged"></telerik:RadDatePicker>
    <asp:Label runat="server" ID="label1"></asp:Label>

    C#:
    protected void RadDatePicker2_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
    {
        int Days = RadDatePicker2.SelectedDate.Value.Subtract(RadDatePicker1.SelectedDate.Value).Days;
        label1.Text = "the difference is: " + Days.ToString();
    }

    Regards,
    Vasil
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. <span>w</span>
    <span>w</span> avatar
    84 posts
    Member since:
    Jun 2012

    Posted 14 Dec 2012 Link to this post

    Thanks Vasil
        your code works like charm. I prefer other to use Vasil code. because the below code does not work properly when you are putting value in some textbox which Enabled property is false. if textbox has enabled property is not false, then you can use below code.
    ie; Enabled="false"

    ASPX:
    <telerik:RadDatePicker ID="txtStartDate" runat="server">
         <ClientEvents OnDateSelected="DateSelected1" />
    </telerik:RadDatePicker>

    <telerik:RadDatePicker ID="txtEndDate" runat="server" >
         <ClientEvents OnDateSelected="DateSelected2" />                                    
    </telerik:RadDatePicker>

    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function DateSelected1(sender, eventArgs) {
                var datePicker1 = $find("<%= txtStartDate.ClientID %>");
                var date1 = datePicker1.get_selectedDate();

                var datePicker2 = $find("<%= txtEndDate.ClientID %>");
                var date2 = datePicker2.get_selectedDate();

                if (date1 <= date2) {
                    var one_day = 1000 * 60 * 60 * 24;
                    alert(Math.ceil((date2.getTime() - date1.getTime()) / (one_day)));
                }
            }
            
            function DateSelected2(sender, eventArgs) {

                var datePicker1 = $find("<%= txtStartDate.ClientID %>");
                var date1 = datePicker1.get_selectedDate();

                var datePicker2 = $find("<%= txtEndDate.ClientID %>");
                var date2 = datePicker2.get_selectedDate();

                if (date1 <= date2) {
                    var one_day = 1000 * 60 * 60 * 24;
                    var totalDay = Math.ceil((date2.getTime() - date1.getTime()) / (one_day));
                    alert(Math.ceil((date2.getTime() - date1.getTime()) / (one_day)));
                }
            }
           
        </script>
    </telerik:RadCodeBlock>

    Regards,
    Muhammad Waseem
    Telerik Team
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017