查看: 359|回复: 0

[.NET开发] C#时间操作类分享

发表于 2018-2-9 08:00:00

本文实例为大家分享了C#时间操作类的具体代码,供大家参考,具体内容如下

  1. using System;
  2. namespace DotNet.Utilities
  3. {
  4. /// <summary>
  5. /// 时间类
  6. /// 1、SecondToMinute(int Second) 把秒转换成分钟
  7. /// </summary>
  8. public class TimeHelper
  9. {
  10. /// <summary>
  11. /// 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间
  12. /// </summary>
  13. /// <param name="dt">年月日分隔符</param>
  14. /// <param name="Separator"></param>
  15. /// <returns></returns>
  16. public string GetFormatDate(DateTime dt, char Separator)
  17. {
  18. if (dt != null && !dt.Equals(DBNull.Value))
  19. {
  20. string tem = string.Format("yyyy{0}MM小贝dd", Separator, Separator);
  21. return dt.ToString(tem);
  22. }
  23. else
  24. {
  25. return GetFormatDate(DateTime.Now, Separator);
  26. }
  27. }
  28. /// <summary>
  29. /// 将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间
  30. /// </summary>
  31. /// <param name="dt"></param>
  32. /// <param name="Separator"></param>
  33. /// <returns></returns>
  34. public string GetFormatTime(DateTime dt, char Separator) {
  35. if (dt != null && !dt.Equals(DBNull.Value))
  36. {
  37. string tem = string.Format("hh{0}mm小贝ss", Separator, Separator);
  38. return dt.ToString(tem);
  39. }
  40. else
  41. {
  42. return GetFormatDate(DateTime.Now, Separator);
  43. }
  44. }
  45. /// <summary>
  46. /// 把秒转换成分钟
  47. /// </summary>
  48. /// <returns></returns>
  49. public static int SecondToMinute(int Second)
  50. {
  51. decimal mm = (decimal)((decimal)Second / (decimal)60);
  52. return Convert.ToInt32(Math.Ceiling(mm));
  53. }
  54. #region 返回某年某月最后一天
  55. /// <summary>
  56. /// 返回某年某月最后一天
  57. /// </summary>
  58. /// <param name="year">年份</param>
  59. /// <param name="month">月份</param>
  60. /// <returns>日</returns>
  61. public static int GetMonthLastDate(int year, int month)
  62. {
  63. DateTime lastDay = new DateTime(year, month, new System.Globalization.GregorianCalendar().GetDaysInMonth(year, month));
  64. int Day = lastDay.Day;
  65. return Day;
  66. }
  67. #endregion
  68. #region 返回时间差
  69. public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
  70. {
  71. string dateDiff = null;
  72. try
  73. {
  74. //TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
  75. //TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
  76. //TimeSpan ts = ts1.Subtract(ts2).Duration();
  77. TimeSpan ts = DateTime2 - DateTime1;
  78. if (ts.Days >= 1)
  79. {
  80. dateDiff = DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日";
  81. }
  82. else
  83. {
  84. if (ts.Hours > 1)
  85. {
  86. dateDiff = ts.Hours.ToString() + "小时前";
  87. }
  88. else
  89. {
  90. dateDiff = ts.Minutes.ToString() + "分钟前";
  91. }
  92. }
  93. }
  94. catch
  95. { }
  96. return dateDiff;
  97. }
  98. #endregion
  99. #region 获得两个日期的间隔
  100. /// <summary>
  101. /// 获得两个日期的间隔
  102. /// </summary>
  103. /// <param name="DateTime1">日期一。</param>
  104. /// <param name="DateTime2">日期二。</param>
  105. /// <returns>日期间隔TimeSpan。</returns>
  106. public static TimeSpan DateDiff2(DateTime DateTime1, DateTime DateTime2)
  107. {
  108. TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
  109. TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
  110. TimeSpan ts = ts1.Subtract(ts2).Duration();
  111. return ts;
  112. }
  113. #endregion
  114. #region 格式化日期时间
  115. /// <summary>
  116. /// 格式化日期时间
  117. /// </summary>
  118. /// <param name="dateTime1">日期时间</param>
  119. /// <param name="dateMode">显示模式</param>
  120. /// <returns>0-9种模式的日期</returns>
  121. public static string FormatDate(DateTime dateTime1, string dateMode)
  122. {
  123. switch (dateMode)
  124. {
  125. case "0":
  126. return dateTime1.ToString("yyyy-MM-dd");
  127. case "1":
  128. return dateTime1.ToString("yyyy-MM-dd HH:mm:ss");
  129. case "2":
  130. return dateTime1.ToString("yyyy/MM/dd");
  131. case "3":
  132. return dateTime1.ToString("yyyy年MM月dd日");
  133. case "4":
  134. return dateTime1.ToString("MM-dd");
  135. case "5":
  136. return dateTime1.ToString("MM/dd");
  137. case "6":
  138. return dateTime1.ToString("MM月dd日");
  139. case "7":
  140. return dateTime1.ToString("yyyy-MM");
  141. case "8":
  142. return dateTime1.ToString("yyyy/MM");
  143. case "9":
  144. return dateTime1.ToString("yyyy年MM月");
  145. default:
  146. return dateTime1.ToString();
  147. }
  148. }
  149. #endregion
  150. #region 得到随机日期
  151. /// <summary>
  152. /// 得到随机日期
  153. /// </summary>
  154. /// <param name="time1">起始日期</param>
  155. /// <param name="time2">结束日期</param>
  156. /// <returns>间隔日期之间的 随机日期</returns>
  157. public static DateTime GetRandomTime(DateTime time1, DateTime time2)
  158. {
  159. Random random = new Random();
  160. DateTime minTime = new DateTime();
  161. DateTime maxTime = new DateTime();
  162. System.TimeSpan ts = new System.TimeSpan(time1.Ticks - time2.Ticks);
  163. // 获取两个时间相隔的秒数
  164. double dTotalSecontds = ts.TotalSeconds;
  165. int iTotalSecontds = 0;
  166. if (dTotalSecontds > System.Int32.MaxValue)
  167. {
  168. iTotalSecontds = System.Int32.MaxValue;
  169. }
  170. else if (dTotalSecontds < System.Int32.MinValue)
  171. {
  172. iTotalSecontds = System.Int32.MinValue;
  173. }
  174. else
  175. {
  176. iTotalSecontds = (int)dTotalSecontds;
  177. }
  178. if (iTotalSecontds > 0)
  179. {
  180. minTime = time2;
  181. maxTime = time1;
  182. }
  183. else if (iTotalSecontds < 0)
  184. {
  185. minTime = time1;
  186. maxTime = time2;
  187. }
  188. else
  189. {
  190. return time1;
  191. }
  192. int maxValue = iTotalSecontds;
  193. if (iTotalSecontds <= System.Int32.MinValue)
  194. maxValue = System.Int32.MinValue + 1;
  195. int i = random.Next(System.Math.Abs(maxValue));
  196. return minTime.AddSeconds(i);
  197. }
  198. #endregion
  199. }
  200. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

您可能感兴趣的文章:

  • 深入Unix时间戳与C# DateTime时间类型互换的详解
  • asp.net(C#)实现功能强大的时间日期处理类完整实例
  • C# string格式的日期时间字符串转为DateTime类型的方法


回复

使用道具 举报