鼎鼎知识库
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

19.C#中关于时间.md 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. >关于年的问题
  2. - DownList显示年,可根据当前的年份,具体代码
  3. ```
  4. public List<string> GetYears()
  5. {
  6. List<string> allYears = new List<string>();
  7. var now = DateTime.Now;
  8. int currentYear = now.Year;//2019
  9. allYears.Add((currentYear - 3).ToString());//2016
  10. allYears.Add((currentYear - 2).ToString());//2017
  11. allYears.Add((currentYear - 1).ToString());//2018
  12. allYears.Add(currentYear.ToString());//2019
  13. allYears.Add((currentYear + 1).ToString());//2020
  14. allYears.Add((currentYear + 2).ToString());//2021
  15. allYears.Add((currentYear + 3).ToString());//2022
  16. return allYears;
  17. }
  18. ```
  19. > 关于月的问题
  20. - DownList显示月。因为月是具体的24个月,可直接增添值
  21. ```
  22. public List<string> GetMonths()
  23. {
  24. List<string> allMonths = new List<string>();
  25. allMonths.AddRange(new List<string> { "01","02","03","04","05","06","07","08","09","10","11","12"});
  26. return allMonths;
  27. }
  28. ```
  29. > 关于天的问题
  30. - 因为每年的每个月的天数不确定,可以根据请求或者选择的年月进行显示天数
  31. ```
  32. /// <summary>
  33. /// 获取天
  34. /// </summary>
  35. /// <param name="year"></param>
  36. /// <param name="month"></param>
  37. /// <returns></returns>
  38. public List<string> GetDays(string year, string month)
  39. {
  40. var result = new List<string>();
  41. if(!string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(month))
  42. {
  43. //拼接月和年 2019-04
  44. string yearAndMonthStr = year + "-" + month;
  45. //月初 2019-04-01
  46. DateTime beginTime = DateTime.Parse(yearAndMonthStr + "-01");
  47. DateTime endTime = DateTime.Parse(beginTime.AddMonths(1).AddDays(-1).ToShortDateString());
  48. //月末
  49. for (DateTime dt = beginTime; dt <= endTime; dt = dt.AddDays(1))
  50. {
  51. result.Add(dt.Day.ToString());
  52. }
  53. }
  54. return result;
  55. }
  56. ```
  57. > 时间格式转换(12或者24小时)
  58. ```
  59. string time = Convert.ToDateTime(date).ToString("yyyy-MM-dd HH:mm");//HH为24小时
  60. string time = Convert.ToDateTime(date).ToString("yyyy-MM-dd hh:mm");//hh为12小时
  61. ```