我应该在 Excel 表中附加开始日期和结束日期。开始日期将是最后几周的星期六,结束日期将是当前几周的星期五。这应该每周进行一次。只有在每个星期一,新的开始和结束日期应该自动附加。我怎样才能得到符合我要求的日期。请帮我。提前致谢。
问问题
189 次
3 回答
1
试试这个
DateTime startOfWeek = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek);
DateTime lastWeekSaturday = StartOfWeek.AddDays(-1);
//Console.Write(lastWeekSaturday);
DateTime currentWeekFriday= DateTime.Now.AddDays(5 - (int)DateTime.Now.DayOfWeek);
//Console.Write(currentWeekFriday);
于 2013-09-16T11:22:19.060 回答
1
如果您的代码仅在星期一运行,您可以使用:
DateTime lastWeekSaturday = DateTime.Now.AddDays(-2);
DateTime thisWeekFriday = DateTime.Now.AddDays(4);
您可以从这些 DateTime 对象中提取您想要的部分。
于 2013-09-16T11:52:44.147 回答
0
这是一些神奇的代码:D
if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
{
DateTime lastWeekSaturday = DateTime.Now.AddDays((int)DayOfWeek.Saturday - 1 - 7);
DateTime lastWeekThursday = DateTime.Now.AddDays((int)DayOfWeek.Thursday - 1 - 7);
DateTime lastWeekTuesday = DateTime.Now.AddDays((int)DayOfWeek.Tuesday - 1 - 7);
DateTime thisWeekFriday = DateTime.Now.AddDays((int)DayOfWeek.Friday - 1);
DateTime thisWeekThursday = DateTime.Now.AddDays((int)DayOfWeek.Thursday - 1);
DateTime thisWeekTuesday = DateTime.Now.AddDays((int)DayOfWeek.Tuesday - 1);
DateTime nextWeekTuesday = DateTime.Now.AddDays((int)DayOfWeek.Tuesday - 1 + 7);
}
else
MessageBox.Show("Its not monday");
只是-7
回到一个星期,然后+7
再前进一个星期。我认为您对我提供的示例会很好。
于 2013-09-16T12:14:01.970 回答