00-Extensions.DateTime.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Text;
  3. namespace LeaRun.Util.Extension
  4. {
  5. /// <summary>
  6. /// 日期时间扩展
  7. /// </summary>
  8. public static partial class Extensions
  9. {
  10. /// <summary>
  11. /// 获取格式化字符串,带时分秒,格式:"yyyy-MM-dd HH:mm:ss"
  12. /// </summary>
  13. /// <param name="dateTime">日期</param>
  14. /// <param name="isRemoveSecond">是否移除秒</param>
  15. public static string ToDateTimeString(this DateTime dateTime, bool isRemoveSecond = false)
  16. {
  17. if (isRemoveSecond)
  18. return dateTime.ToString("yyyy-MM-dd HH:mm");
  19. return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
  20. }
  21. /// <summary>
  22. /// 获取格式化字符串,带时分秒,格式:"yyyy-MM-dd HH:mm:ss"
  23. /// </summary>
  24. /// <param name="dateTime">日期</param>
  25. /// <param name="isRemoveSecond">是否移除秒</param>
  26. public static string ToDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
  27. {
  28. if (dateTime == null)
  29. return string.Empty;
  30. return ToDateTimeString(dateTime.Value, isRemoveSecond);
  31. }
  32. /// <summary>
  33. /// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
  34. /// </summary>
  35. /// <param name="dateTime">日期</param>
  36. public static string ToDateString(this DateTime dateTime)
  37. {
  38. return dateTime.ToString("yyyy-MM-dd");
  39. }
  40. /// <summary>
  41. /// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
  42. /// </summary>
  43. /// <param name="dateTime">日期</param>
  44. public static string ToDateString(this DateTime? dateTime)
  45. {
  46. if (dateTime == null)
  47. return string.Empty;
  48. return ToDateString(dateTime.Value);
  49. }
  50. /// <summary>
  51. /// 获取格式化字符串,不带年月日,格式:"HH:mm:ss"
  52. /// </summary>
  53. /// <param name="dateTime">日期</param>
  54. public static string ToTimeString(this DateTime dateTime)
  55. {
  56. return dateTime.ToString("HH:mm:ss");
  57. }
  58. /// <summary>
  59. /// 获取格式化字符串,不带年月日,格式:"HH:mm:ss"
  60. /// </summary>
  61. /// <param name="dateTime">日期</param>
  62. public static string ToTimeString(this DateTime? dateTime)
  63. {
  64. if (dateTime == null)
  65. return string.Empty;
  66. return ToTimeString(dateTime.Value);
  67. }
  68. /// <summary>
  69. /// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff"
  70. /// </summary>
  71. /// <param name="dateTime">日期</param>
  72. public static string ToMillisecondString(this DateTime dateTime)
  73. {
  74. return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  75. }
  76. /// <summary>
  77. /// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff"
  78. /// </summary>
  79. /// <param name="dateTime">日期</param>
  80. public static string ToMillisecondString(this DateTime? dateTime)
  81. {
  82. if (dateTime == null)
  83. return string.Empty;
  84. return ToMillisecondString(dateTime.Value);
  85. }
  86. /// <summary>
  87. /// 获取格式化字符串,不带时分秒,格式:"yyyy年MM月dd日"
  88. /// </summary>
  89. /// <param name="dateTime">日期</param>
  90. public static string ToChineseDateString(this DateTime dateTime)
  91. {
  92. return string.Format("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day);
  93. }
  94. /// <summary>
  95. /// 获取格式化字符串,不带时分秒,格式:"yyyy年MM月dd日"
  96. /// </summary>
  97. /// <param name="dateTime">日期</param>
  98. public static string ToChineseDateString(this DateTime? dateTime)
  99. {
  100. if (dateTime == null)
  101. return string.Empty;
  102. return ToChineseDateString(dateTime.SafeValue());
  103. }
  104. /// <summary>
  105. /// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分"
  106. /// </summary>
  107. /// <param name="dateTime">日期</param>
  108. /// <param name="isRemoveSecond">是否移除秒</param>
  109. public static string ToChineseDateTimeString(this DateTime dateTime, bool isRemoveSecond = false)
  110. {
  111. StringBuilder result = new StringBuilder();
  112. result.AppendFormat("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day);
  113. result.AppendFormat(" {0}时{1}分", dateTime.Hour, dateTime.Minute);
  114. if (isRemoveSecond == false)
  115. result.AppendFormat("{0}秒", dateTime.Second);
  116. return result.ToString();
  117. }
  118. /// <summary>
  119. /// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分"
  120. /// </summary>
  121. /// <param name="dateTime">日期</param>
  122. /// <param name="isRemoveSecond">是否移除秒</param>
  123. public static string ToChineseDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
  124. {
  125. if (dateTime == null)
  126. return string.Empty;
  127. return ToChineseDateTimeString(dateTime.Value);
  128. }
  129. }
  130. }