获取C#中两个特定日期时间截止日期前3周

时间:2016-02-26 10:03:22

标签: c# datetime week-number

我有两个日期作为开始日期和结束日期。我需要在结束日期/截止日期前3周发送提醒。

var start= DateTime.Now;
var end = new DateTime(2016, 10, 25);
var timeParts = StartEndDate.Split(new char[1] { ':' });

var eventstartDate = new DateTime(start.Year, start.Month, start.Day,
int.Parse(timeParts[0]), int.Parse(timeParts[1]), int.Parse(timeParts[2]));


var eventEnDate = new DateTime(end .Year, end .Month, end .Day,
int.Parse(timeParts[0]), int.Parse(timeParts[1]), int.Parse(timeParts[2]));

TimeSpan ts = eventEnDate - eventstartDate;

int totaldays = ts.Days;

int totalWeeks = totaldays / 7;

//Add 7 days for each loop until end date
for (DateTime ds= start; ds< eventEnDate; ds= ds.AddDays(7))
{
    //DO something so to get last 3 week prior to end date to set reminder
}

1 个答案:

答案 0 :(得分:2)

结束日期前3周是21天。

var endDateMinusThreeWeeks = end.AddDays(-21);