三元财务API

DateTimeConvert.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. namespace CallCenter.Utility
  3. {
  4. /// <summary>
  5. ///DateTimeConvert
  6. /// </summary>
  7. public class DateTimeConvert
  8. {
  9. private static readonly DateTime StartedDateTime = new DateTime(1970, 1, 1,0,0,0,DateTimeKind.Utc);
  10. /// <summary>
  11. /// 根据阿拉伯数字返回月份的名称(可更改为某种语言)
  12. /// </summary>
  13. public static string[] Monthes
  14. {
  15. get
  16. {
  17. return new[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  18. }
  19. }
  20. /// <summary>
  21. /// 判断是不是当月的最后一天,是返回true,否返回flase
  22. /// </summary>
  23. /// <param name="Dt"></param>
  24. /// <returns></returns>
  25. public bool IsLastMonthDay(DateTime Dt)
  26. {
  27. bool Flag = false;
  28. DateTime MonthClearTime = Dt.AddDays(1 - Dt.Day).AddMonths(1).AddDays(-1);
  29. if (DateDiff("D", Dt, MonthClearTime) == 0)
  30. {
  31. Flag = true;
  32. }
  33. return Flag;
  34. }
  35. #region 日期时间方法
  36. public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
  37. {
  38. string dateDiff = null;
  39. TimeSpan ts = DateTime1.Subtract(DateTime2).Duration();
  40. string days = "";string hours = "";
  41. if (ts.Days.ToString() != "0")
  42. days = ts.Days.ToString() + "天";
  43. if(ts.Hours.ToString() !="0")
  44. hours = ts.Hours.ToString() + "时";
  45. dateDiff = days + hours + ts.Minutes.ToString() + "分"; // + ts.Seconds.ToString() + "秒";
  46. return dateDiff;
  47. }
  48. /// <summary>
  49. /// 日期时间相减方法 M 月,D 天,H小时,T分,S秒
  50. /// </summary>
  51. /// <param name="dtype"></param>
  52. /// <param name="D1"></param>
  53. /// <param name="D2"></param>
  54. /// <returns></returns>
  55. public static int DateDiff(string dtype, DateTime D1, DateTime D2)
  56. {
  57. int d = 0;
  58. int h = 0;
  59. int t = 0;
  60. switch (dtype.ToUpper())
  61. {
  62. case "M":
  63. d = D2.Year - D1.Year;
  64. h = D2.Month - D1.Month;
  65. if (d > 0)
  66. {
  67. return d * 12 + h + 1;
  68. }
  69. if (d == 0)
  70. {
  71. if (h > 0)
  72. {
  73. return h + 1;
  74. }
  75. if (h == 0)
  76. {
  77. return 1;
  78. }
  79. return d * 12 + h - 1;
  80. }
  81. return d * 12 + h - 1;
  82. case "D":
  83. TimeSpan Num = Convert.ToDateTime(D2.ToShortDateString()) - Convert.ToDateTime(D1.ToShortDateString());
  84. return Num.Days;
  85. case "H":
  86. d = DateDiff("D", D1, D2);
  87. h = D2.Hour - D1.Hour;
  88. return h + (d * 24);
  89. case "T":
  90. d = Convert.ToInt32(D2.Subtract(D1).Days);
  91. h = Convert.ToInt32(D2.Subtract(D1).Hours);
  92. t = Convert.ToInt32(D2.Subtract(D1).Minutes);
  93. return ((h + (d * 24)) * 60) + t;
  94. case "S":
  95. d = Convert.ToInt32(D2.Subtract(D1).Days);
  96. h = Convert.ToInt32(D2.Subtract(D1).Hours);
  97. t = Convert.ToInt32(D2.Subtract(D1).Minutes);
  98. return ((((h + (d * 24)) * 60) + t) * 60) + Convert.ToInt32(D2.Subtract(D1).Seconds);
  99. default:
  100. return -1;
  101. }
  102. }
  103. public static long DateDiff(DateInterval Interval, DateTime StartDate, DateTime EndDate)
  104. {
  105. long lngDateDiffValue = 0;
  106. var TS = new TimeSpan(EndDate.Ticks - StartDate.Ticks);
  107. switch (Interval)
  108. {
  109. case DateInterval.Second:
  110. lngDateDiffValue = (long)TS.TotalSeconds;
  111. break;
  112. case DateInterval.Minute:
  113. lngDateDiffValue = (long)TS.TotalMinutes;
  114. break;
  115. case DateInterval.Hour:
  116. lngDateDiffValue = (long)TS.TotalHours;
  117. break;
  118. case DateInterval.Day:
  119. lngDateDiffValue = TS.Days;
  120. break;
  121. case DateInterval.Week:
  122. lngDateDiffValue = TS.Days / 7;
  123. break;
  124. case DateInterval.Month:
  125. lngDateDiffValue = TS.Days / 30;
  126. break;
  127. case DateInterval.Quarter:
  128. lngDateDiffValue = (TS.Days / 30) / 3;
  129. break;
  130. case DateInterval.Year:
  131. lngDateDiffValue = TS.Days / 365;
  132. break;
  133. }
  134. return (lngDateDiffValue);
  135. }
  136. #endregion
  137. /// <summary>
  138. /// 将时间戳转换为DateTime类型
  139. /// </summary>
  140. /// <param name="timetag"></param>
  141. /// <param name="toLocalTime"></param>
  142. /// <returns></returns>
  143. public static DateTime FromTimetag(long timetag,bool toLocalTime = true)
  144. {
  145. var time = new DateTime(StartedDateTime.Ticks + timetag*10000);
  146. return toLocalTime ? time.ToLocalTime() : time;
  147. }
  148. /// <summary>
  149. /// 将Datetime类型转换为时间戳
  150. /// </summary>
  151. /// <param name="dateTime"></param>
  152. /// <returns></returns>
  153. public static long ToTimetag(DateTime dateTime)
  154. {
  155. return (dateTime.ToUniversalTime().Ticks - StartedDateTime.Ticks)/10000;
  156. }
  157. ///<summary>
  158. ///由秒数得到日期几天几小时。。。
  159. ///</summary
  160. ///<param name="t">秒数</param>
  161. ///<param name="type">0:转换后带秒,1:转换后不带秒</param>
  162. ///<returns>几天几小时几分几秒</returns>
  163. public static string parseTimeSeconds(int t, int type)
  164. {
  165. string r = "";
  166. int day, hour, minute, second;
  167. if (t >= 86400) //天,
  168. {
  169. day = Convert.ToInt32(t / 86400);
  170. hour = Convert.ToInt32((t % 86400) / 3600);
  171. minute = Convert.ToInt32((t % 86400 % 3600) / 60);
  172. second = Convert.ToInt32(t % 86400 % 3600 % 60);
  173. if (type == 0)
  174. r = day + ("天") + hour + ("小时") + minute + ("分") + second + ("秒");
  175. else
  176. r = day + ("天") + hour + ("小时") + minute + ("分");
  177. }
  178. else if (t >= 3600)//时,
  179. {
  180. hour = Convert.ToInt32(t / 3600);
  181. minute = Convert.ToInt32((t % 3600) / 60);
  182. second = Convert.ToInt32(t % 3600 % 60);
  183. if (type == 0)
  184. r = hour + ("小时") + minute + ("分") + second + ("秒");
  185. else
  186. r = hour + ("小时") + minute + ("分");
  187. }
  188. else if (t >= 60)//分
  189. {
  190. minute = Convert.ToInt32(t / 60);
  191. second = Convert.ToInt32(t % 60);
  192. r = minute + ("分") + second + ("秒");
  193. }
  194. else
  195. {
  196. second = Convert.ToInt32(t);
  197. r = second + ("秒");
  198. }
  199. return r;
  200. }
  201. /// <summary>
  202. /// 当前日期时间,转换为秒
  203. /// </summary>
  204. /// <returns>秒数</returns>
  205. public static string xDateSeconds()
  206. {
  207. long xdateseconds = 0;
  208. DateTime xdatenow = DateTime.UtcNow; //当前UTC时间
  209. long xminute = 60; //一分种60秒
  210. long xhour = 3600;
  211. long xday = 86400;
  212. long byear = 1970;//从1970-1-1 0:00:00开始到现在所过的秒
  213. long[] xmonth = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  214. long[] xyear = { 365, 366 };
  215. long num = 0;
  216. xdateseconds += xdatenow.Second; //算秒
  217. xdateseconds += xdatenow.Minute * xminute; //算分
  218. xdateseconds += xdatenow.Hour * xhour; //算时
  219. xdateseconds += (xdatenow.Day - 1) * xday; //算天
  220. //算月(月换成天算)
  221. if (DateTime.IsLeapYear(xdatenow.Year))
  222. {
  223. xdateseconds += (xmonth[xdatenow.Month - 1] + 1) * xday;
  224. }
  225. else
  226. {
  227. xdateseconds += (xmonth[xdatenow.Month - 1]) * xday;
  228. }
  229. //算年(年换成天算)
  230. long lyear = xdatenow.Year - byear;
  231. for (int i = 0; i < lyear; i++)
  232. {
  233. if (DateTime.IsLeapYear((int)byear + i))
  234. {
  235. num++;
  236. }
  237. }
  238. xdateseconds += ((lyear - num) * xyear[0] + num * xyear[1]) * xday;
  239. return xdateseconds.ToString();
  240. }
  241. /// <summary>
  242. /// 秒数转日期
  243. /// </summary>
  244. /// <param name="Value">秒数</param>
  245. /// <returns>日期</returns>
  246. public static DateTime GetGMTDateTime(int Value)
  247. {
  248. //秒数转时间日期
  249. //GMT时间从2000年1月1日开始,先把它作为赋为初值
  250. long Year = 2000, Month = 1, Day = 1;
  251. long Hour = 0, Min = 0, Sec = 0;
  252. //临时变量
  253. long iYear = 0, iDay = 0;
  254. long iHour = 0, iMin = 0, iSec = 0;
  255. //计算文件创建的年份
  256. iYear = Value / (365 * 24 * 60 * 60);
  257. Year = Year + iYear;
  258. //计算文件除创建整年份以外还有多少天
  259. iDay = (Value % (365 * 24 * 60 * 60)) / (24 * 60 * 60);
  260. //把闰年的年份数计算出来
  261. int RInt = 0;
  262. for (int i = 2000; i < Year; i++)
  263. {
  264. if ((i % 4) == 0)
  265. RInt = RInt + 1;
  266. }
  267. //计算文件创建的时间(几时)
  268. iHour = ((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) / (60 * 60);
  269. Hour = Hour + iHour;
  270. //计算文件创建的时间(几分)
  271. iMin = (((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) % (60 * 60)) / 60;
  272. Min = Min + iMin;
  273. //计算文件创建的时间(几秒)
  274. iSec = (((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) % (60 * 60)) % 60;
  275. Sec = Sec + iSec;
  276. DateTime t = new DateTime((int)Year, (int)Month, (int)Day, (int)Hour, (int)Min, (int)Sec);
  277. DateTime t1 = t.AddDays(iDay - RInt);
  278. return t1;
  279. }
  280. /// <summary>
  281. /// 将c# DateTime时间格式转换为Unix时间戳格式
  282. /// </summary>
  283. /// <param name="time">时间</param>
  284. /// <returns>long</returns>
  285. public static long ConvertDateTimeToInt(System.DateTime time)
  286. {
  287. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
  288. long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
  289. return t;
  290. }
  291. }
  292. }