C# 两个日期相减核算月份

淩亂°似流年 2023-02-23 12:18 100阅读 0赞
  1. #region 两个日期相减得月份
  2. public string GetMonth(string startTime, string endTime)
  3. {
  4. string msg = string.Empty;
  5. DateTime startDate = DateTime.Parse(startTime);
  6. DateTime endDate = DateTime.Parse(endTime); //结束时间-起始时间
  7. int totalMonth = endDate.Year * 12 + endDate.Month - startDate.Year * 12 - startDate.Month;
  8. if (totalMonth < 3)
  9. {
  10. msg = "3个月以下";
  11. }
  12. else if (totalMonth >= 3 & totalMonth < 12)
  13. {
  14. msg = "3月-1年";
  15. }
  16. else if (totalMonth >= 12 & totalMonth < 24)
  17. {
  18. msg = "1年-2年";
  19. }
  20. else if (totalMonth >= 24 & totalMonth < 36)
  21. {
  22. msg = "2年-3年";
  23. }
  24. else if (totalMonth >= 36 & totalMonth < 48)
  25. {
  26. msg = "3年-4年";
  27. }
  28. else if (totalMonth >= 48)
  29. {
  30. msg = "5年以上";
  31. }
  32. return msg;
  33. }
  34. #endregion

int totalMonth = endDate.Year * 12 + endDate.Month - startDate.Year * 12 - startDate.Month;

* 核算:开始日期-结束日期=月份数

发表评论

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

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

相关阅读

    相关 oracle 时间

    oracle 两个时间相减默认的是天数 oracle 两个时间相减默认的是天数 \ 24 为相差的小时数 oracle 两个时间相减默认的是天数 \ 24 \ 60 为相差