C# 获取周一、周日

柔情只为你懂 2022-06-11 08:19 1387阅读 0赞

一、按国内计算周一为一周的起点,周日为一周的终点

1.获取周一

  1. //获取周一
  2. private DateTime getMonday()
  3. {
  4. DateTime now = DateTime.Now;
  5. DateTime temp = new DateTime(now.Year, now.Month, now.Day);
  6. int count = now.DayOfWeek - DayOfWeek.Monday;
  7. if (count == -1) count = 6;
  8. return temp.AddDays(-count);
  9. }

2.获取周日

  1. //获取周天
  2. private DateTime getSunday()
  3. {
  4. DateTime now = DateTime.Now;
  5. DateTime temp = new DateTime(now.Year, now.Month, now.Day);
  6. int count = now.DayOfWeek - DayOfWeek.Sunday;
  7. if (count != 0) count = 7 - count;
  8. return temp.AddDays(count);
  9. }

二、DayOfWeek定义如下:

  1. //
  2. // 摘要:
  3. // Specifies the day of the week.
  4. [ComVisible(true)]
  5. public enum DayOfWeek
  6. {
  7. //
  8. // 摘要:
  9. // Indicates Sunday.
  10. Sunday = 0,
  11. //
  12. // 摘要:
  13. // Indicates Monday.
  14. Monday = 1,
  15. //
  16. // 摘要:
  17. // Indicates Tuesday.
  18. Tuesday = 2,
  19. //
  20. // 摘要:
  21. // Indicates Wednesday.
  22. Wednesday = 3,
  23. //
  24. // 摘要:
  25. // Indicates Thursday.
  26. Thursday = 4,
  27. //
  28. // 摘要:
  29. // Indicates Friday.
  30. Friday = 5,
  31. //
  32. // 摘要:
  33. // Indicates Saturday.
  34. Saturday = 6
  35. }

更多:

C# dynamic常用整理

C# int类型的强制转换整理

C# 扩展方法整理

发表评论

表情:
评论列表 (有 0 条评论,1387人围观)

还没有评论,来说两句吧...

相关阅读