DataTimeHelper.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Bowin.Common.DataTime
  6. {
  7. /// <summary>
  8. /// 时间类
  9. /// </summary>
  10. public class DataTimeHelper
  11. {
  12. #region 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间
  13. /// <summary>
  14. /// 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间
  15. /// </summary>
  16. /// <param name="dt">年月日分隔符</param>
  17. /// <param name="Separator"></param>
  18. /// <returns></returns>
  19. ///
  20. public string GetFormatDate(DateTime dt, char Separator)
  21. {
  22. if (dt != null && !dt.Equals(DBNull.Value))
  23. {
  24. string tem = string.Format("yyyy{0}MM{1}dd", Separator, Separator);
  25. return dt.ToString(tem);
  26. }
  27. else
  28. {
  29. return GetFormatDate(DateTime.Now, Separator);
  30. }
  31. }
  32. #endregion
  33. #region 将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间
  34. /// <summary>
  35. /// 将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间
  36. /// </summary>
  37. /// <param name="dt"></param>
  38. /// <param name="Separator"></param>
  39. /// <returns></returns>
  40. public string GetFormatTime(DateTime dt, char Separator)
  41. {
  42. if (dt != null && !dt.Equals(DBNull.Value))
  43. {
  44. string tem = string.Format("hh{0}mm{1}ss", Separator, Separator);
  45. return dt.ToString(tem);
  46. }
  47. else
  48. {
  49. return GetFormatDate(DateTime.Now, Separator);
  50. }
  51. }
  52. #endregion
  53. #region 把秒转换成分钟
  54. /// <summary>
  55. /// 把秒转换成分钟
  56. /// </summary>
  57. /// <returns></returns>
  58. public static int SecondToMinute(int Second)
  59. {
  60. decimal mm = (decimal)((decimal)Second / (decimal)60);
  61. return Convert.ToInt32(Math.Ceiling(mm));
  62. }
  63. #endregion
  64. #region 返回某年某月最后一天
  65. /// <summary>
  66. /// 返回某年某月最后一天
  67. /// </summary>
  68. /// <param name="year">年份</param>
  69. /// <param name="month">月份</param>
  70. /// <returns>日</returns>
  71. public static int GetMonthLastDate(int year, int month)
  72. {
  73. DateTime lastDay = new DateTime(year, month, new System.Globalization.GregorianCalendar().GetDaysInMonth(year, month));
  74. int Day = lastDay.Day;
  75. return Day;
  76. }
  77. #endregion
  78. #region 返回时间差
  79. public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
  80. {
  81. string dateDiff = null;
  82. try
  83. {
  84. //TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
  85. //TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
  86. //TimeSpan ts = ts1.Subtract(ts2).Duration();
  87. TimeSpan ts = DateTime2 - DateTime1;
  88. if (ts.Days >= 1)
  89. {
  90. dateDiff = DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日";
  91. }
  92. else
  93. {
  94. if (ts.Hours > 1)
  95. {
  96. dateDiff = ts.Hours.ToString() + "小时前";
  97. }
  98. else
  99. {
  100. dateDiff = ts.Minutes.ToString() + "分钟前";
  101. }
  102. }
  103. }
  104. catch
  105. { }
  106. return dateDiff;
  107. }
  108. #endregion
  109. #region 获得两个日期的间隔
  110. /// <summary>
  111. /// 获得两个日期的间隔
  112. /// </summary>
  113. /// <param name="DateTime1">日期一。</param>
  114. /// <param name="DateTime2">日期二。</param>
  115. /// <returns>日期间隔TimeSpan。</returns>
  116. public static TimeSpan DateDiff2(DateTime DateTime1, DateTime DateTime2)
  117. {
  118. TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
  119. TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
  120. TimeSpan ts = ts1.Subtract(ts2).Duration();
  121. return ts;
  122. }
  123. #endregion
  124. #region 格式化日期时间
  125. /// <summary>
  126. /// 格式化日期时间
  127. /// </summary>
  128. /// <param name="dateTime1">日期时间</param>
  129. /// <param name="dateMode">显示模式</param>
  130. /// <returns>0-9种模式的日期</returns>
  131. public static string FormatDate(DateTime dateTime1, string dateMode)
  132. {
  133. switch (dateMode)
  134. {
  135. case "0":
  136. return dateTime1.ToString("yyyy-MM-dd");
  137. case "1":
  138. return dateTime1.ToString("yyyy-MM-dd HH:mm:ss");
  139. case "2":
  140. return dateTime1.ToString("yyyy/MM/dd");
  141. case "3":
  142. return dateTime1.ToString("yyyy年MM月dd日");
  143. case "4":
  144. return dateTime1.ToString("MM-dd");
  145. case "5":
  146. return dateTime1.ToString("MM/dd");
  147. case "6":
  148. return dateTime1.ToString("MM月dd日");
  149. case "7":
  150. return dateTime1.ToString("yyyy-MM");
  151. case "8":
  152. return dateTime1.ToString("yyyy/MM");
  153. case "9":
  154. return dateTime1.ToString("yyyy年MM月");
  155. default:
  156. return dateTime1.ToString();
  157. }
  158. }
  159. #endregion
  160. #region 得到随机日期
  161. /// <summary>
  162. /// 得到随机日期
  163. /// </summary>
  164. /// <param name="time1">起始日期</param>
  165. /// <param name="time2">结束日期</param>
  166. /// <returns>间隔日期之间的 随机日期</returns>
  167. public static DateTime GetRandomTime(DateTime time1, DateTime time2)
  168. {
  169. Random random = new Random();
  170. DateTime minTime = new DateTime();
  171. DateTime maxTime = new DateTime();
  172. System.TimeSpan ts = new System.TimeSpan(time1.Ticks - time2.Ticks);
  173. // 获取两个时间相隔的秒数
  174. double dTotalSecontds = ts.TotalSeconds;
  175. int iTotalSecontds = 0;
  176. if (dTotalSecontds > System.Int32.MaxValue)
  177. {
  178. iTotalSecontds = System.Int32.MaxValue;
  179. }
  180. else if (dTotalSecontds < System.Int32.MinValue)
  181. {
  182. iTotalSecontds = System.Int32.MinValue;
  183. }
  184. else
  185. {
  186. iTotalSecontds = (int)dTotalSecontds;
  187. }
  188. if (iTotalSecontds > 0)
  189. {
  190. minTime = time2;
  191. maxTime = time1;
  192. }
  193. else if (iTotalSecontds < 0)
  194. {
  195. minTime = time1;
  196. maxTime = time2;
  197. }
  198. else
  199. {
  200. return time1;
  201. }
  202. int maxValue = iTotalSecontds;
  203. if (iTotalSecontds <= System.Int32.MinValue)
  204. maxValue = System.Int32.MinValue + 1;
  205. int i = random.Next(System.Math.Abs(maxValue));
  206. return minTime.AddSeconds(i);
  207. }
  208. #endregion
  209. #region Excel数字转换时间格式
  210. /// <summary>
  211. /// Excel数字转换时间格式
  212. /// </summary>
  213. /// <param name="timeStr">数字,如:42583对应文本格式日期2016-08-01 </param>
  214. /// <returns>日期/时间格式</returns>
  215. public string ToDateTimeValue(string strNumber)
  216. {
  217. if (!string.IsNullOrWhiteSpace(strNumber))
  218. {
  219. Decimal tempValue;
  220. //先检查 是不是数字;
  221. if (Decimal.TryParse(strNumber, out tempValue))
  222. {
  223. //天数,取整
  224. int day = Convert.ToInt32(Math.Truncate(tempValue));
  225. //这里也不知道为什么. 如果是小于32,则减1,否则减2
  226. //日期从1900-01-01开始累加
  227. // day = day < 32 ? day - 1 : day - 2;
  228. DateTime dt = new DateTime(1900, 1, 1).AddDays(day < 32 ? (day - 1) : (day - 2));
  229. //小时:减掉天数,这个数字转换小时:(* 24)
  230. Decimal hourTemp = (tempValue - day) * 24;//获取小时数
  231. //取整.小时数
  232. int hour = Convert.ToInt32(Math.Truncate(hourTemp));
  233. //分钟:减掉小时,( * 60)
  234. //这里舍入,否则取值会有1分钟误差.
  235. Decimal minuteTemp = Math.Round((hourTemp - hour) * 60, 2);//获取分钟数
  236. int minute = Convert.ToInt32(Math.Truncate(minuteTemp));
  237. //秒:减掉分钟,( * 60)
  238. //这里舍入,否则取值会有1秒误差.
  239. Decimal secondTemp = Math.Round((minuteTemp - minute) * 60, 2);//获取秒数
  240. int second = Convert.ToInt32(Math.Truncate(secondTemp));
  241. //时间格式:00:00:00
  242. string resultTimes = string.Format("{0}:{1}:{2}",
  243. (hour < 10 ? ("0" + hour) : hour.ToString()),
  244. (minute < 10 ? ("0" + minute) : minute.ToString()),
  245. (second < 10 ? ("0" + second) : second.ToString()));
  246. if (day > 0)
  247. return string.Format("{0} {1}", dt.ToString("yyyy-MM-dd"), resultTimes);
  248. else
  249. return resultTimes;
  250. }
  251. }
  252. return string.Empty;
  253. }
  254. #endregion
  255. }
  256. }