Determine if the weekend will be a weekend

I need to check if the selected date is selected from the datepicker on the weekend. The function should continue to check if the new start date is a holiday. You also need to add days to the start if the weekend comes.

The code should be something like this:

int startday = Datepicker1.SelectedDate; if (startdate = weekendday, startdate++) { startdate++ //or if a sunday +2 } else { return startdate } 

Thank you for your help.

+7
source share
5 answers
 if (startdate.DayOfWeek == DayOfWeek.Saturday) startdate = startdate.AddDays(2); else if (startdate.DayOfWeek == DayOfWeek.Sunday) startdate = startdate.AddDays(1); 
+23
source

Using the DayOfWeek property, you can explicitly check the weekend. Something like that:

 if ((startDate.DayOfWeek == DayOfWeek.Saturday) || (startDate.DayOfWeek == DayOfWeek.Sunday)) 

Of course, this is a bit arbitrary. Abstracting it to a helper method makes it a little cleaner:

 private bool IsWeekend(DateTime date) { return (date.DayOfWeek == DayOfWeek.Saturday) || (date.DayOfWeek == DayOfWeek.Sunday) } 

To use the following:

 if (IsWeekend(startDate)) 

Or maybe a little cleaner, you could write an extension method for DateTime :

 public static bool IsWeekend(this DateTime date) { return (date.DayOfWeek == DayOfWeek.Saturday) || (date.DayOfWeek == DayOfWeek.Sunday) } 

What you will use as follows:

 if (startDate.IsWeekend()) 
+4
source

See DateTime.DayOfWeek documented here .

 while(startday.DayOfWeek == DayOfWeek.Saturday || startday.DayOfWeek == DayOfWeek.Sunday) { startday = startday.AddDays(1); } 
0
source

You can simply use the DateTime.DayOfWeek property. Good example from MSDN: http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek.aspx

0
source

Look at the DateTime.DayOfWeek property - it will give you the day of the week your DateTime object falls on.

0
source

All Articles