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

selecting number of days from two raddatepicker in a textbox

4 Answers 106 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Prashanth
Top achievements
Rank 1
Prashanth asked on 31 Aug 2012, 02:42 PM
Hi all,

 I am new to telerik controls, I am having two RadDatePickers(FromRadDatePicker and ToRadDatePicker) and a TextBox(TotalTextBox).I want the Number of days from the Two raddatepickers to the Textbox. I am getting it by using this below code
TimeSpan ts = ToRadDatePicker.SelectedDate.Value.Subtract(FromRadDatePicker.SelectedDate.Value);
           TotalTextBox.Text = ts.Days.ToString();
I also want subtracting the saturdays and sundays and custom holiday list if any.

For Example:-

FromRadDatePicker  31-Aug-2012
ToRadDatePiker     05-Sep-2012

The TotalTextBox Should be 2( 1 and 2-Sep is saturday, sunday and 4-sep is a custom created festival Holiday)

How to create Custom Created Holiday List, and Subtracting the Saturday and Sunday?

Any one Please help me out.

4 Answers, 1 is accepted

Sort by
0
Prashanth
Top achievements
Rank 1
answered on 31 Aug 2012, 04:43 PM
any one please help me out.........
0
Prashanth
Top achievements
Rank 1
answered on 01 Sep 2012, 10:18 AM
Is there any one who can do this..
0
Prashanth
Top achievements
Rank 1
answered on 04 Sep 2012, 05:49 AM
It would be odd if i reply to my post again and again but the thing is it is urgent for me.
0
Princy
Top achievements
Rank 2
answered on 04 Sep 2012, 08:32 AM
Hi Prashanth,

You can add Custom holidays as special days. Here is the sample code that I tried.

ASPX
<telerik:RadDatePicker ID="FromRadDatePicker" runat="server" onselecteddatechanged="RadDatePicker_SelectedDateChanged" AutoPostBack="true" ></telerik:RadDatePicker>
<telerik:RadDatePicker ID="ToRadDatePicker" runat="server" onselecteddatechanged="RadDatePicker_SelectedDateChanged" AutoPostBack="true" ></telerik:RadDatePicker>
<telerik:RadTextBox ID="TotalTextBox" runat="server"></telerik:RadTextBox>

C#:
protected void Page_Load(object sender, EventArgs e)
{
 if (!IsPostBack)
 {
   // Adding Custom Holidays
  RadCalendarDay calendarDay1 = new RadCalendarDay();
  DateTime dt1 = new DateTime(2012, 06, 20);
  calendarDay1.Date = dt1.Date;
  FromRadDatePicker.Calendar.SpecialDays.Add(calendarDay1);
 
  FromRadDatePicker.SelectedDate = DateTime.Today;
  ToRadDatePicker.SelectedDate = DateTime.Today;
 }
}
 
public static int Weekdays(DateTime dtmStart, DateTime dtmEnd)
{
 if (dtmStart >= dtmEnd)
 {
   return 0;
 }
 int dowStart = ((int)dtmStart.DayOfWeek == 0 ? 7 : (int)dtmStart.DayOfWeek);
 int dowEnd = ((int)dtmEnd.DayOfWeek == 0 ? 7 : (int)dtmEnd.DayOfWeek);
 TimeSpan tSpan = dtmEnd - dtmStart;
 if (dowStart <= dowEnd)
 {
  return (((tSpan.Days / 7) * 5) + Math.Max((Math.Min((dowEnd + 1), 6) - dowStart), 0));
 }
 return (((tSpan.Days / 7) * 5) + Math.Min((dowEnd + 6) - Math.Min(dowStart, 6), 5));
}
 
protected void RadDatePicker_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
{
 int SplDays = 0;
 foreach (RadCalendarDay day in FromRadDatePicker.Calendar.SpecialDays)
 {
  if ((day.Date.CompareTo(FromRadDatePicker.SelectedDate) >= 0) && (day.Date.CompareTo(ToRadDatePicker.SelectedDate) <= 0) && (!day.IsWeekend))
  {
    SplDays ++;
  }
 }
 int weekdays = Weekdays(FromRadDatePicker.SelectedDate.Value, ToRadDatePicker.SelectedDate.Value);
 int Total = weekdays - SplDays;
 TotalTextBox.Text = Total.ToString();
}

Hope this helps.

Thanks,
Princy.
Tags
General Discussions
Asked by
Prashanth
Top achievements
Rank 1
Answers by
Prashanth
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or