This is a migrated thread and some comments may be shown as answers.

RadDatePicker c# count days between dates

5 Answers 549 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Piseth
Top achievements
Rank 2
Piseth asked on 03 Oct 2012, 04:01 PM
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.

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 04 Oct 2012, 05:05 AM
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.
0
Waseem
Top achievements
Rank 1
answered on 14 Dec 2012, 01:24 PM
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.


0
Vasil
Telerik team
answered on 14 Dec 2012, 01:58 PM
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.
0
Waseem
Top achievements
Rank 1
answered on 14 Dec 2012, 03:41 PM
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
0
jack
Top achievements
Rank 1
Iron
answered on 16 Feb 2023, 04:12 PM | edited on 16 Feb 2023, 04:14 PM
Hi everyone. I’m looking for a formula or a tool like a day counter type to make the following difference: today’s date minus the item’s creation date. I would like the result in days. For example, if the item was created on 1/1/2021 and today is 1/7/2021 I would like the result “6”. Thanks!
Rumen
Telerik team
commented on 21 Feb 2023, 12:11 PM

The solutions for RadDatePicker posted here demonstrates exactly how to count the days between a selection of two dates.

You can use a simple formula to calculate the difference between two dates in days. Here's the formula:

(Number of days between the two dates) = (Current date) - (Item creation date)

You can even use the formula in Excel and Google Sheets to calculate the number of days between two dates.

Tags
Calendar
Asked by
Piseth
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Waseem
Top achievements
Rank 1
Vasil
Telerik team
jack
Top achievements
Rank 1
Iron
Share this question
or