c#中是否有内置函数进行比较比较今天的日期时间以匹配当前月份的(第一,第二,第三,第五,第五)工作日(周一,周二等)
或任何人都可以提供相同的自定义解决方案。
此致
答案 0 :(得分:2)
嗯,你可以很容易地知道这一天是否匹配:
// Note: consider time zones...
DateTime today = DateTime.Today;
if (today.DayOfWeek == DayOfWeek.Monday)
{
...
}
并且您知道一周中每天的第一次出现将在1-7范围内,第二次出现在8-14等范围内。所以:
// Check if it's the second Friday of the month...
int targetOccurrence = 2;
DayOfWeek targetDay = DayOfWeek.Friday;
DateTime today = DateTime.Today;
if (today.DayOfWeek == targetDay &&
(today.Day + 6) / 7 == targetOccurrence)
{
// Yes, it's the second Friday
}
如果你想知道它是在本月的第二个星期五之前还是之后,那就更难了。无论如何都不是不可能,但更加繁琐。
答案 1 :(得分:1)
您可以使用
DateTime.Now.DayOfWeek
要获取当周的当天:
DayOfWeek dToday = DateTime.Now.DayOfWeek;
int iDay = dToday.GetHashCode(); // a number between 0-6 representing
// the days in the week
string sDayName = dToday.ToString(); // can be either Sunday, Monday .. Satruday
答案 2 :(得分:0)
使用DateTime.DayOfWeek属性。
e.g。
if (DateTime.Now.DayOfWeek == DayOfWeek.Monday)
{
Console.WriteLine("Someone's got a case of the Mondays!");
}