我试图获得一周中的某一天只有当输入日期在上周一到当前日期之间,即如果今天是30/10/14,则输出应该是星期四,否则只显示输入的日期 这是我正在尝试的
DateTime aa = Convert.ToDateTime(rr.detail);
if (DateTime.Now.Subtract(aa).Days < 7)
{
// but this is not i am looking for i want only if aa
// is falling between last monday and less than 7 days
}
所以任何想法如何实现它?
答案 0 :(得分:1)
如果我理解正确,我采取的步骤是:
1)获取上周一的日期
2)获取用户的日期
3)检查用户的日期是否在上周一之后(在这种情况下) 显示当天)
//Get the date of the previous Monday
DateTime prevMonday = DateTime.Now;
while(prevMonday.DayOfWeek != DayOfWeek.Monday)
prevMonday = prevMonday.AddDays(-1);
//get user's date
DateTime aa = Convert.ToDateTime(rr.detail);
//check if the user's date is after the Monday
if (aa > prevMonday && aa <= DateTime.Now.Date)
Console.WriteLine(aa.DayOfWeek);
else
Console.WriteLine(aa);