Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How chould i jump over satuday and sunday if that the day and still add 6 days from startdate today until end date + 6 days

Calendar c = CultureInfo.CurrentCulture.Calendar;
                   int year = DateTime.Now.Year;
                   int month = DateTime.Now.Month;
                   int days= DateTime.Now.Day;


                   DateTime StartDate = new DateTime(year,month,days);

                   DateTime EndDate = DateTime.Now.AddDays(6);
                   int DayInterval = 1;

                   List<DateTime> dateList = new List<DateTime>();
                   while (StartDate <= EndDate)
                   {


                       StartDate = StartDate.AddDays(DayInterval);
                       dateList.Add(StartDate);


                   }



UPDATE
====
if (StartDate.DayOfWeek == DayOfWeek.Sunday || StartDate.DayOfWeek == DayOfWeek.Saturday)
                       {

                       }

                       else
                       {
                           StartDate = StartDate.AddDays(DayInterval);
                           dateList.Add(StartDate);
                       }


Like this?=
Posted
Updated 19-Mar-15 1:03am
v2
Comments
Sergey Alexandrovich Kryukov 19-Mar-15 7:24am    
First of all, what do you want to achieve? Your code has so many problems that it makes no sense to trust it.
—SA
Kurac1 19-Mar-15 7:36am    
Hi i have the and endate, startdate it datetime.now and enddate is 6 days later, so for example today its thursday so plus 6 days from today will get wednesday but it should jump over saturday and sunday

Please see my comment to the question. It is not clear what you are trying to achieve. I can only answer how to skip some days, but it will work only if if you have some loop where some variable (loop variable) are actually assigned to the time with the day of weeks to be skipped, for example, if you increment by one day or less.

Not sure if you want that, but to illustrate just the "jump", it could be anything like
C#
System.DateTime currentDate = //...
System.DateTime finalDate = //...
while (currentDate <= finalDate) { // just for example
    if (currentDate.DayOfWeek == System.DayOfWeek.Saturday ||
        currentDate.DayOfWeek == System.DayOfWeek.Sunday)
           continue; // this is your "jump"
    DoSomethingWithYourTime(currentDate);
    currentDate = IncrementTimeSomehow(currentDate); // for example AddDays...
}
I hope you got the idea, not apply some logic. If you just need to count days not counting weekends (I'm not sure), you would need to count number of full weeks and multiply by 5. At the beginning and the end, you can have incomplete weeks, for them, you would need to check if Sunday or Saturday fall in the interval of each of incomplete weeks, so you will need to subtract 0, 1 or two days from each. If you deal with just few days (say, a month of few month, an year), just iterating by one day could be simpler. Just use logic.

—SA
 
Share this answer
 
v4
Comments
Kurac1 20-Mar-15 3:47am    
For jump i mean is if adding 6 days from today, it should not add days saturday and sunday it should display friday, monday,tuesday,wednesday,tuesday,friday
C#
You need 6 days.

int DaysCount = 0;
while(DaysCount < 6)
{
    if (StarDate.DayOfWeek != DayOfWeek.Saturday 
        && StarDate.DayOfWeek != DayOfWeek.Sunday)
    {
        dateList.Add(SartDate);
        DaysCount++;
    }
}
 
Share this answer
 
v2
Try this, assuming startDate may also fall on weekends:
using System;
					
public class Program
{
	public static void Main()
	{
		DateTime startDate = new DateTime(2015,3,24 ); // DateTime.Now;
		DateTime endDate = startDate;
		
		for (int i=0;i < 7; i++){
			int dayOfWeek = (int) endDate.DayOfWeek;
			int add = 1;
			if (dayOfWeek == 6 || dayOfWeek == 7) 
			{
				add++; // skip one more day on weekend
			}
			endDate = endDate.AddDays(add);
		}		
			Console.WriteLine(endDate);			
	}
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900