如何跳过周六和周日 [英] How to jump over saturday and sunday

查看:298
本文介绍了如何跳过周六和周日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果那天仍然可以跳过satuday和星期天,并且从今天的开始日期到结束日期+ 6天仍然增加6天



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

====




UPDATE
====

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

                       }

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





喜欢这个?=



Like this?=

推荐答案

请参阅我对该问题的评论。目前尚不清楚你想要实现的目标。我只能回答如何跳过某些日子,但只有当你有一些循环,其中一些变量(循环变量)实际分配给要跳过的星期几的时间时才会起作用,例如,如果你增加一天或更短时间。



不确定你是否想要那个,但为了说明跳跃,它可能就像

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
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...
}

我希望你有了主意,没有应用一些逻辑。如果你只需要计算不计算周末的天数(我不确定),你需要计算整周的数量并乘以5.在开始和结束时,你可以有不完整的周,对于他们,你会需要检查周日或周六是否在每个不完整周的间隔内,所以你需要从每个周期中减去0,1或两天。如果你只处理几天(比如一个月,一个月,一年),只需迭代一天就可以更简单了。只需使用逻辑。



-SA

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


You need 6 days.

int DaysCount = 0;
while(DaysCount < 6)
{
    if (StarDate.DayOfWeek != DayOfWeek.Saturday 
        && StarDate.DayOfWeek != DayOfWeek.Sunday)
    {
        dateList.Add(SartDate);
        DaysCount++;
    }
}


试试这个,假设startDate也可能在周末出现:

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);			
	}
}


这篇关于如何跳过周六和周日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆