纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.5 KiB

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