0

How do I get the values of days (Sunday to Saturday) using DatePart in .NET? My idea is, if it is a Saturday or Sunday, I have to skip the loop.

MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65

3 Answers3

3
while (true)
{
   if (DateTime.Now.DayOfWeek == DayOfWeek.Saturday
       || DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
   {
       break;
   }
}
Waqas Raja
  • 10,802
  • 4
  • 33
  • 38
  • the thing is I have a fromDate and a toDate. I have to loop within this range, find out if its a Saturday/Sunday and if that's the case, I got to break the loop. – MusicLovingIndianGirl Apr 16 '13 at 07:24
  • Can you please post your code? So that we could make the correction(s). – Waqas Raja Apr 16 '13 at 08:07
  • `Dim stDate As DateTime = txtFromDate.Text Dim enDate As DateTime = txtToDate.Text Dim stDay As Integer = 2 While stDate <= enDate If DatePart(DateInterval.Weekday, stDate) = 7 Or DatePart(DateInterval.Weekday, stDate) = 6 Then GoTo Inc_DateInfo End If For p = 2 To gvTimeTable.Columns.Count - 2 Dim day = DatePart(DateInterval.Day, stDate) ` – MusicLovingIndianGirl Apr 16 '13 at 08:28
1

So for a date range you could try:

for(var curr_date = fromDate; curr_date <= toDate; curr_date.AddDays(1))
{
  if (curr_date.DayOfWeek == DayOfWeek.Saturday || 
      curr_date.DayOfWeek == DayOfWeek.Sunday)
  {
     break;
  }      
}
mickfold
  • 2,003
  • 1
  • 14
  • 20
0
 string s = DateTime.Now.DayOfWeek.ToString();
    if (s == "Saturday")
    {
      //Your code     
    }

you can add your condition appropriate to the application

Debasish
  • 68
  • 7