地铁二期项目正式开始

DateUtil.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. using System;
  2. using System.Data;
  3. namespace YTSoft.Common
  4. {
  5. [Serializable]
  6. public enum DateInterval
  7. {
  8. Second, Minute, Hour, Day, Week, Month, Quarter, Year
  9. }
  10. /// <summary>
  11. /// 日期处理工具类
  12. /// </summary>
  13. [Serializable]
  14. public class DateUtil
  15. {
  16. private DateUtil() { }
  17. private static readonly string ISO8601Short = "yyyy-MM-dd";
  18. private static readonly string ISO8601Long = "yyyy-MM-dd HH:mm:ss";
  19. /// <summary>
  20. /// 根据日期获取一个周期(7天)的开始日期
  21. /// </summary>
  22. /// <param name="cd"></param>
  23. /// <returns></returns>
  24. public static DateTime getStartDate(DateTime cd)
  25. {
  26. string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
  27. switch (Day[Convert.ToInt16(DateTime.Now.DayOfWeek)])
  28. {
  29. case "星期日": return cd.AddDays(-9);
  30. case "星期一": return cd.AddDays(-10);
  31. case "星期二": return cd.AddDays(-11);
  32. case "星期三": return cd.AddDays(-12);
  33. case "星期四": return cd.AddDays(-13);
  34. case "星期五": return cd.AddDays(-7);
  35. case "星期六": return cd.AddDays(-8);
  36. }
  37. return cd;
  38. }
  39. /// <summary>
  40. /// 根据日期获取一个周期(7天)的结束日期
  41. /// </summary>
  42. /// <param name="cd"></param>
  43. /// <returns></returns>
  44. public static DateTime getEndDate(DateTime cd)
  45. {
  46. int t = Convert.ToInt16(cd.DayOfWeek);
  47. switch (t)
  48. {
  49. case 0: return cd.AddDays(-3);
  50. case 1: return cd.AddDays(-4);
  51. case 2: return cd.AddDays(-5);
  52. case 3: return cd.AddDays(-6);
  53. case 4: return cd.AddDays(-7);
  54. case 5: return cd.AddDays(-1);
  55. case 6: return cd.AddDays(-2);
  56. }
  57. return cd;
  58. }
  59. /// <summary>
  60. /// 判断 字符对象 是否是日期字符
  61. /// </summary>
  62. /// <param name="obj">字符对象</param>
  63. /// <returns></returns>
  64. public static bool isDate(object obj)
  65. {
  66. bool tag = false;
  67. try
  68. {
  69. if (obj != null)
  70. {
  71. Convert.ToDateTime(obj.ToString());
  72. tag = true;
  73. }
  74. }
  75. catch (Exception)
  76. {
  77. }
  78. return tag;
  79. }
  80. public static string toStr(object obj)
  81. {
  82. try
  83. {
  84. if (obj == null || obj == DBNull.Value)
  85. {
  86. return string.Empty;
  87. }
  88. else
  89. {
  90. return Convert.ToDateTime(obj.ToString()).ToString(DateUtil.ISO8601Short);
  91. }
  92. }
  93. catch (Exception)
  94. {
  95. return string.Empty;
  96. }
  97. }
  98. public static string toLongStr(object obj)
  99. {
  100. try
  101. {
  102. if (obj == null || obj == DBNull.Value)
  103. {
  104. return string.Empty;
  105. }
  106. else
  107. {
  108. return Convert.ToDateTime(obj.ToString()).ToString(DateUtil.ISO8601Long);
  109. }
  110. }
  111. catch (Exception)
  112. {
  113. return string.Empty;
  114. }
  115. }
  116. /// <summary>
  117. /// 将日期对象转换成日期
  118. /// </summary>
  119. /// <param name="obj"></param>
  120. /// <returns></returns>
  121. public static DateTime toDate(object obj)
  122. {
  123. try
  124. {
  125. if (obj != null)
  126. {
  127. return Convert.ToDateTime(obj.ToString().Trim());
  128. }
  129. else
  130. {
  131. return DateTime.MinValue;
  132. }
  133. }
  134. catch (Exception)
  135. {
  136. return DateTime.MinValue;
  137. }
  138. }
  139. /// <summary>
  140. /// 实现类似数据库 DateDiff 功能
  141. /// </summary>
  142. /// <param name="Interval">间隔类型</param>
  143. /// <param name="StartDate">开始日期</param>
  144. /// <param name="EndDate">结束日期</param>
  145. /// <returns></returns>
  146. public static long DateDiff(DateInterval Interval, System.DateTime StartDate, System.DateTime EndDate)
  147. {
  148. long lngDateDiffValue = 0;
  149. System.TimeSpan TS = new System.TimeSpan(EndDate.Ticks - StartDate.Ticks);
  150. switch (Interval)
  151. {
  152. case DateInterval.Second:
  153. lngDateDiffValue = (long)TS.TotalSeconds;
  154. break;
  155. case DateInterval.Minute:
  156. lngDateDiffValue = (long)TS.TotalMinutes;
  157. break;
  158. case DateInterval.Hour:
  159. lngDateDiffValue = (long)TS.TotalHours;
  160. break;
  161. case DateInterval.Day:
  162. lngDateDiffValue = (long)TS.Days;
  163. break;
  164. case DateInterval.Week:
  165. lngDateDiffValue = (long)(TS.Days / 7);
  166. break;
  167. case DateInterval.Month:
  168. lngDateDiffValue = (long)(TS.Days / 30);
  169. break;
  170. case DateInterval.Quarter:
  171. lngDateDiffValue = (long)((TS.Days / 30) / 3);
  172. break;
  173. case DateInterval.Year:
  174. lngDateDiffValue = (long)(TS.Days / 365);
  175. break;
  176. }
  177. return (lngDateDiffValue);
  178. }
  179. public static string toColorStr(object obj)
  180. {
  181. string temp = "";
  182. try
  183. {
  184. if (obj != null)
  185. {
  186. DateTime dt = Convert.ToDateTime(obj.ToString());
  187. if (dt.Year == 2011)
  188. {
  189. temp = "<span class='x-grid-back-red1'>" + dt.ToString(DateUtil.ISO8601Short) + "</span>";
  190. }
  191. if (dt.Year == 2012)
  192. {
  193. temp = "<span class='x-grid-back-red2'>" + dt.ToString(DateUtil.ISO8601Short) + "</span>";
  194. }
  195. if (dt.Year == 2013)
  196. {
  197. temp = "<span class='x-grid-back-red3'>" + dt.ToString(DateUtil.ISO8601Short) + "</span>";
  198. }
  199. if (dt.Year == 2014)
  200. {
  201. temp = "<span class='x-grid-back-red4'>" + dt.ToString(DateUtil.ISO8601Short) + "</span>";
  202. }
  203. }
  204. }
  205. catch (Exception)
  206. {
  207. }
  208. return temp;
  209. }
  210. /// <summary>
  211. /// 获取该年中是第几周
  212. /// </summary>
  213. /// <param name="day">日期</param>
  214. /// <returns></returns>
  215. public static int WeekOfYear(System.DateTime day)
  216. {
  217. int weeknum;
  218. System.DateTime fDt = DateTime.Parse(day.Year.ToString() + "-01-01");
  219. int k = Convert.ToInt32(fDt.DayOfWeek);//得到该年的第一天是周几
  220. if (k == 0)
  221. {
  222. k = 7;
  223. }
  224. int l = Convert.ToInt32(day.DayOfYear);//得到当天是该年的第几天
  225. l = l - (7 - k + 1);
  226. if (l <= 0)
  227. {
  228. weeknum = 1;
  229. }
  230. else
  231. {
  232. if (l % 7 == 0)
  233. {
  234. weeknum = l / 7 + 1;
  235. }
  236. else
  237. {
  238. weeknum = l / 7 + 2;//不能整除的时候要加上前面的一周和后面的一周
  239. }
  240. }
  241. return weeknum;
  242. }
  243. /// <summary>
  244. /// 获取该年中是第几季度
  245. /// </summary>
  246. /// <param name="day">日期</param>
  247. /// <returns></returns>
  248. public static int QuarterOfYear(System.DateTime day)
  249. {
  250. if (day.Month < 4)
  251. {
  252. return 1;
  253. }
  254. else if (day.Month > 9)
  255. {
  256. return 4;
  257. }
  258. else if (day.Month > 3 && day.Month < 7)
  259. {
  260. return 2;
  261. }
  262. else
  263. {
  264. return 3;
  265. }
  266. }
  267. /// <summary>
  268. /// 计算日期间隔
  269. /// </summary>
  270. /// <param name="d1">要参与计算的其中一个日期字符串</param>
  271. /// <param name="d2">要参与计算的另一个日期字符串</param>
  272. /// <returns>一个表示日期间隔的TimeSpan类型</returns>
  273. public static TimeSpan toResult(string d1, string d2)
  274. {
  275. try
  276. {
  277. DateTime date1 = DateTime.Parse(d1);
  278. DateTime date2 = DateTime.Parse(d2);
  279. return toResult(date1, date2);
  280. }
  281. catch
  282. {
  283. throw new Exception("字符串参数不正确!");
  284. }
  285. }
  286. /// <summary>
  287. /// 计算日期间隔
  288. /// </summary>
  289. /// <param name="d1">要参与计算的其中一个日期</param>
  290. /// <param name="d2">要参与计算的另一个日期</param>
  291. /// <returns>一个表示日期间隔的TimeSpan类型</returns>
  292. public static TimeSpan toResult(DateTime d1, DateTime d2)
  293. {
  294. TimeSpan ts;
  295. if (d1 > d2)
  296. {
  297. ts = d1 - d2;
  298. }
  299. else
  300. {
  301. ts = d2 - d1;
  302. }
  303. return ts;
  304. }
  305. /// <summary>
  306. /// 计算日期间隔
  307. /// </summary>
  308. /// <param name="d1">要参与计算的其中一个日期字符串</param>
  309. /// <param name="d2">要参与计算的另一个日期字符串</param>
  310. /// <param name="drf">决定返回值形式的枚举</param>
  311. /// <returns>一个代表年月日的int数组,具体数组长度与枚举参数drf有关</returns>
  312. public static int[] toResult(string d1, string d2, diffResultFormat drf)
  313. {
  314. try
  315. {
  316. DateTime date1 = DateTime.Parse(d1);
  317. DateTime date2 = DateTime.Parse(d2);
  318. return toResult(date1, date2, drf);
  319. }
  320. catch
  321. {
  322. throw new Exception("字符串参数不正确!");
  323. }
  324. }
  325. /// <summary>
  326. /// 计算日期间隔
  327. /// </summary>
  328. /// <param name="d1">要参与计算的其中一个日期</param>
  329. /// <param name="d2">要参与计算的另一个日期</param>
  330. /// <param name="drf">决定返回值形式的枚举</param>
  331. /// <returns>一个代表年月日的int数组,具体数组长度与枚举参数drf有关</returns>
  332. public static int[] toResult(DateTime d1, DateTime d2, diffResultFormat drf)
  333. {
  334. #region 数据初始化
  335. DateTime max;
  336. DateTime min;
  337. int year;
  338. int month;
  339. int tempYear, tempMonth;
  340. if (d1 > d2)
  341. {
  342. max = d1;
  343. min = d2;
  344. }
  345. else
  346. {
  347. max = d2;
  348. min = d1;
  349. }
  350. tempYear = max.Year;
  351. tempMonth = max.Month;
  352. if (max.Month < min.Month)
  353. {
  354. tempYear--;
  355. tempMonth = tempMonth + 12;
  356. }
  357. year = tempYear - min.Year;
  358. month = tempMonth - min.Month;
  359. #endregion
  360. #region 按条件计算
  361. if (drf == diffResultFormat.dd)
  362. {
  363. TimeSpan ts = max - min;
  364. return new int[] { ts.Days };
  365. }
  366. if (drf == diffResultFormat.mm)
  367. {
  368. return new int[] { month + year * 12 };
  369. }
  370. if (drf == diffResultFormat.yy)
  371. {
  372. return new int[] { year };
  373. }
  374. return new int[] { year, month };
  375. #endregion
  376. }
  377. /// <summary>
  378. /// 获取指定年月的最后一天
  379. /// </summary>
  380. /// <param name="year">年份</param>
  381. /// <param name="month">月份</param>
  382. /// <returns></returns>
  383. public static int GetLastDayofMonth(int year, int month)
  384. {
  385. int days = DateTime.DaysInMonth(year, month);
  386. DateTime datetime = new DateTime(year, month, 1);
  387. return datetime.AddDays(days - 1).Day;
  388. }
  389. /// <summary>
  390. /// 获取指定日期所在周的第一天,星期天为第一天
  391. /// </summary>
  392. /// <param name="dateTime"></param>
  393. /// <returns></returns>
  394. public static DateTime GetDateTimeWeekFirstDaySun(DateTime dateTime)
  395. {
  396. DateTime firstWeekDay = DateTime.Now;
  397. try
  398. {
  399. //得到是星期几,然后从当前日期减去相应天数
  400. int weeknow = Convert.ToInt32(dateTime.DayOfWeek);
  401. int daydiff = (-1) * weeknow;
  402. firstWeekDay = dateTime.AddDays(daydiff);
  403. }
  404. catch { }
  405. return firstWeekDay;
  406. }
  407. /// <summary>
  408. /// 获取指定日期所在周的第一天,星期一为第一天
  409. /// </summary>
  410. /// <param name="dateTime"></param>
  411. /// <returns></returns>
  412. public static DateTime GetDateTimeWeekFirstDayMon(DateTime dateTime)
  413. {
  414. DateTime firstWeekDay = DateTime.Now;
  415. try
  416. {
  417. int weeknow = Convert.ToInt32(dateTime.DayOfWeek);
  418. //星期一为第一天,weeknow等于0时,要向前推6天。
  419. weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1));
  420. int daydiff = (-1) * weeknow;
  421. firstWeekDay = dateTime.AddDays(daydiff);
  422. }
  423. catch { }
  424. return firstWeekDay;
  425. }
  426. /// <summary>
  427. /// 获取指定日期所在周的最后一天,星期六为最后一天
  428. /// </summary>
  429. /// <param name="dateTime"></param>
  430. /// <returns></returns>
  431. public static DateTime GetDateTimeWeekLastDaySat(DateTime dateTime)
  432. {
  433. DateTime lastWeekDay = DateTime.Now;
  434. try
  435. {
  436. int weeknow = Convert.ToInt32(dateTime.DayOfWeek);
  437. int daydiff = (7 - weeknow) - 1;
  438. lastWeekDay = dateTime.AddDays(daydiff);
  439. }
  440. catch { }
  441. return lastWeekDay;
  442. }
  443. /// <summary>
  444. /// 获取指定日期所在周的最后一天,星期天为最后一天
  445. /// </summary>
  446. /// <param name="dateTime"></param>
  447. /// <returns></returns>
  448. public static DateTime GetDateTimeWeekLastDaySun(DateTime dateTime)
  449. {
  450. DateTime lastWeekDay = DateTime.Now;
  451. try
  452. {
  453. int weeknow = Convert.ToInt32(dateTime.DayOfWeek);
  454. weeknow = (weeknow == 0 ? 7 : weeknow);
  455. int daydiff = (7 - weeknow);
  456. lastWeekDay = dateTime.AddDays(daydiff);
  457. }
  458. catch { }
  459. return lastWeekDay;
  460. }
  461. /// <summary>
  462. /// 获取指定日期的月份第一天
  463. /// </summary>
  464. /// <param name="dateTime"></param>
  465. /// <returns></returns>
  466. public static DateTime GetDateTimeMonthFirstDay(DateTime dateTime)
  467. {
  468. if (dateTime == null)
  469. {
  470. dateTime = DateTime.Now;
  471. }
  472. return new DateTime(dateTime.Year, dateTime.Month, 1);
  473. }
  474. /// <summary>
  475. /// 获取指定月份最后一天
  476. /// </summary>
  477. /// <param name="dateTime"></param>
  478. /// <returns></returns>
  479. public static DateTime GetDateTimeMonthLastDay(DateTime dateTime)
  480. {
  481. int day = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);
  482. return new DateTime(dateTime.Year, dateTime.Month, day);
  483. }
  484. }
  485. }
  486. /// <summary>
  487. /// 关于返回值形式的枚举
  488. /// </summary>
  489. public enum diffResultFormat
  490. {
  491. /// <summary>
  492. /// 年数和月数
  493. /// </summary>
  494. yymm,
  495. /// <summary>
  496. /// 年数
  497. /// </summary>
  498. yy,
  499. /// <summary>
  500. /// 月数
  501. /// </summary>
  502. mm,
  503. /// <summary>
  504. /// 天数
  505. /// </summary>
  506. dd,
  507. }