我的开始日期为05/03/2012
,持续时间为200
天我现在想要结束日期,不包括星期日。所以我的结束日期应该是05/02/2013
..有人可以帮助我吗
答案 0 :(得分:1)
试试这件事:
var startDate = new DateTime(2012, 5, 3);
var sundaysOverDuration = 200 / 7;
var actualDuration = 200 + sundaysOverDuration;
var newDate = startDate.AddDays(actualDuration);
我还要诚实地承认,this link在处理执行这些类型的计算时存在的大量异常方面非常优雅。我不确定你需要一些复杂的东西,但值得让你知道。我将内联代码,以确保在链接断开时保留它。
public static double GetBusinessDays(DateTime startD, DateTime endD)
{
double calcBusinessDays =
1 + ((endD-startD).TotalDays * 6 -
(startD.DayOfWeek-endD.DayOfWeek) * 2) / 7;
if ((int)startD.DayOfWeek == 0) calcBusinessDays --;
return calcBusinessDays;
}
public static DateTime AddWorkDaysToStartDate(DateTime startD, double businessDays)
{
int DoW = (int)startD.DayOfWeek;
double temp = businessDays + DoW + 1;
if (DoW != 0) temp --;
DateTime calcendD = startD.AddDays(
Math.Floor(temp / 6)*2-DoW + temp
- 2* Convert.ToInt32(temp % 6 == 0)) ;
}
最后,根据您的问题,您似乎不需要处理假期,但如果您这样做,解决方案要复杂得多,需要数据库驱动,所以请记住这一点。
答案 1 :(得分:0)
您可以使用Time Period Library for .NET中的 CalendarDateAdd 类:
// ----------------------------------------------------------------------
public void AddDaysSample()
{
CalendarDateAdd calendarDateAdd = new CalendarDateAdd();
calendarDateAdd.AddWorkingWeekDays();
calendarDateAdd.WeekDays.Add( DayOfWeek.Saturday );
DateTime start = new DateTime( 2012, 5, 3 );
TimeSpan duration = new TimeSpan( 200, 0, 0, 0 );
DateTime? end = calendarDateAdd.Add( start, duration );
Console.WriteLine( "AddDaysSample : {0:d} + {1} days = {2:d}", start, duration.Days, end );
} // AddDaysSample