Utils.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. using Microsoft.AspNetCore.Http;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace HaBookCms.Common
  7. {
  8. public class Utils
  9. {
  10. /// <summary>
  11. /// 生成随机字母字符串(数字字母混和)
  12. /// </summary>
  13. /// <param name="codeCount">待生成的位数</param>
  14. public static string GetCheckCode(int codeCount)
  15. {
  16. string str = string.Empty;
  17. int rep = 0;
  18. long num2 = DateTime.Now.Ticks + rep;
  19. rep++;
  20. Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
  21. for (int i = 0; i < codeCount; i++)
  22. {
  23. char ch;
  24. int num = random.Next();
  25. if ((num % 2) == 0)
  26. {
  27. ch = (char)(0x30 + ((ushort)(num % 10)));
  28. }
  29. else
  30. {
  31. ch = (char)(0x41 + ((ushort)(num % 0x1a)));
  32. }
  33. str = str + ch.ToString();
  34. }
  35. return str;
  36. }
  37. /// <summary>
  38. /// 将字符串转换为int类型数组
  39. /// </summary>
  40. /// <param name="str">如1,2,3,4,5</param>
  41. /// <returns></returns>
  42. public static List<int> StrToListInt(string str)
  43. {
  44. var list = new List<int>();
  45. if (!str.Contains(","))
  46. {
  47. list.Add(int.Parse(str));
  48. return list;
  49. }
  50. var slist = str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  51. foreach (var item in slist)
  52. {
  53. list.Add(int.Parse(item));
  54. }
  55. return list;
  56. }
  57. /// <summary>
  58. /// 将字符串转换为int类型数组
  59. /// </summary>
  60. /// <param name="str">如1,2,3,4,5</param>
  61. /// <returns></returns>
  62. public static List<string> StrToListString(string str)
  63. {
  64. var list = new List<string>();
  65. if (!str.Contains(","))
  66. {
  67. list.Add(str);
  68. return list;
  69. }
  70. var slist = str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  71. foreach (var item in slist)
  72. {
  73. list.Add(item);
  74. }
  75. return list;
  76. }
  77. /// <summary>
  78. /// 将字符串转换为数组
  79. /// </summary>
  80. /// <param name="str">字符串</param>
  81. /// <returns>字符串数组</returns>
  82. public static string[] GetStrArray(string str)
  83. {
  84. return str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  85. }
  86. #region 截取字符串
  87. public static string GetSubString(string pSrcString, int pLength, string pTailString)
  88. {
  89. return GetSubString(pSrcString, 0, pLength, pTailString);
  90. }
  91. public static string GetSubString(string pSrcString, int pStartIndex, int pLength, string pTailString)
  92. {
  93. string str = pSrcString;
  94. byte[] bytes = Encoding.UTF8.GetBytes(pSrcString);
  95. foreach (char ch in Encoding.UTF8.GetChars(bytes))
  96. {
  97. if (((ch > 'ࠀ') && (ch < '一')) || ((ch > 0xac00) && (ch < 0xd7a3)))
  98. {
  99. if (pStartIndex >= pSrcString.Length)
  100. {
  101. return "";
  102. }
  103. return pSrcString.Substring(pStartIndex, ((pLength + pStartIndex) > pSrcString.Length) ? (pSrcString.Length - pStartIndex) : pLength);
  104. }
  105. }
  106. if (pLength < 0)
  107. {
  108. return str;
  109. }
  110. byte[] sourceArray = Encoding.Default.GetBytes(pSrcString);
  111. if (sourceArray.Length <= pStartIndex)
  112. {
  113. return str;
  114. }
  115. int length = sourceArray.Length;
  116. if (sourceArray.Length > (pStartIndex + pLength))
  117. {
  118. length = pLength + pStartIndex;
  119. }
  120. else
  121. {
  122. pLength = sourceArray.Length - pStartIndex;
  123. pTailString = "";
  124. }
  125. int num2 = pLength;
  126. int[] numArray = new int[pLength];
  127. byte[] destinationArray = null;
  128. int num3 = 0;
  129. for (int i = pStartIndex; i < length; i++)
  130. {
  131. if (sourceArray[i] > 0x7f)
  132. {
  133. num3++;
  134. if (num3 == 3)
  135. {
  136. num3 = 1;
  137. }
  138. }
  139. else
  140. {
  141. num3 = 0;
  142. }
  143. numArray[i] = num3;
  144. }
  145. if ((sourceArray[length - 1] > 0x7f) && (numArray[pLength - 1] == 1))
  146. {
  147. num2 = pLength + 1;
  148. }
  149. destinationArray = new byte[num2];
  150. Array.Copy(sourceArray, pStartIndex, destinationArray, 0, num2);
  151. return (Encoding.Default.GetString(destinationArray) + pTailString);
  152. }
  153. #endregion
  154. #region 截取字符长度
  155. /// <summary>
  156. /// 截取字符长度
  157. /// </summary>
  158. /// <param name="inputString">字符</param>
  159. /// <param name="len">长度</param>
  160. /// <returns></returns>
  161. public static string CutString(string inputString, int len)
  162. {
  163. if (string.IsNullOrEmpty(inputString))
  164. return "";
  165. inputString = DropHtml(inputString);
  166. ASCIIEncoding ascii = new ASCIIEncoding();
  167. int tempLen = 0;
  168. string tempString = "";
  169. byte[] s = ascii.GetBytes(inputString);
  170. for (int i = 0; i < s.Length; i++)
  171. {
  172. if ((int)s[i] == 63)
  173. {
  174. tempLen += 2;
  175. }
  176. else
  177. {
  178. tempLen += 1;
  179. }
  180. try
  181. {
  182. tempString += inputString.Substring(i, 1);
  183. }
  184. catch
  185. {
  186. break;
  187. }
  188. if (tempLen > len)
  189. break;
  190. }
  191. //如果截过则加上半个省略号
  192. byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
  193. if (mybyte.Length > len)
  194. tempString += "…";
  195. return tempString;
  196. }
  197. public static string DropHtml(string htmlstring)
  198. {
  199. if (string.IsNullOrEmpty(htmlstring)) return "";
  200. //删除脚本
  201. htmlstring = Regex.Replace(htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
  202. //删除HTML
  203. htmlstring = Regex.Replace(htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
  204. htmlstring = Regex.Replace(htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
  205. htmlstring = Regex.Replace(htmlstring, @"-->", "", RegexOptions.IgnoreCase);
  206. htmlstring = Regex.Replace(htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
  207. htmlstring = Regex.Replace(htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
  208. htmlstring = Regex.Replace(htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
  209. htmlstring = Regex.Replace(htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
  210. htmlstring = Regex.Replace(htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
  211. htmlstring = Regex.Replace(htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
  212. htmlstring = Regex.Replace(htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
  213. htmlstring = Regex.Replace(htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
  214. htmlstring = Regex.Replace(htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
  215. htmlstring = Regex.Replace(htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
  216. htmlstring = Regex.Replace(htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
  217. htmlstring = htmlstring.Replace("<", "");
  218. htmlstring = htmlstring.Replace(">", "");
  219. htmlstring = htmlstring.Replace("\r\n", "");
  220. //htmlstring = HttpContext.Current.Server.HtmlEncode(htmlstring).Trim();
  221. return htmlstring;
  222. }
  223. #endregion
  224. #region 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
  225. /// <summary>
  226. /// 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
  227. /// </summary>
  228. /// <param name="CnChar">单个汉字</param>
  229. /// <returns>单个大写字母</returns>
  230. public static string GetCharSpellCode(string CnChar)
  231. {
  232. long iCnChar;
  233. byte[] ZW = Encoding.Default.GetBytes(CnChar);
  234. //如果是字母,则直接返回首字母
  235. if (ZW.Length == 1)
  236. {
  237. return CutString(CnChar.ToUpper(), 1);
  238. }
  239. else
  240. {
  241. int i1 = (short)(ZW[0]);
  242. int i2 = (short)(ZW[1]);
  243. iCnChar = i1 * 256 + i2;
  244. }
  245. // iCnChar match the constant
  246. if ((iCnChar >= 45217) && (iCnChar <= 45252))
  247. {
  248. return "A";
  249. }
  250. else if ((iCnChar >= 45253) && (iCnChar <= 45760))
  251. {
  252. return "B";
  253. }
  254. else if ((iCnChar >= 45761) && (iCnChar <= 46317))
  255. {
  256. return "C";
  257. }
  258. else if ((iCnChar >= 46318) && (iCnChar <= 46825))
  259. {
  260. return "D";
  261. }
  262. else if ((iCnChar >= 46826) && (iCnChar <= 47009))
  263. {
  264. return "E";
  265. }
  266. else if ((iCnChar >= 47010) && (iCnChar <= 47296))
  267. {
  268. return "F";
  269. }
  270. else if ((iCnChar >= 47297) && (iCnChar <= 47613))
  271. {
  272. return "G";
  273. }
  274. else if ((iCnChar >= 47614) && (iCnChar <= 48118))
  275. {
  276. return "H";
  277. }
  278. else if ((iCnChar >= 48119) && (iCnChar <= 49061))
  279. {
  280. return "J";
  281. }
  282. else if ((iCnChar >= 49062) && (iCnChar <= 49323))
  283. {
  284. return "K";
  285. }
  286. else if ((iCnChar >= 49324) && (iCnChar <= 49895))
  287. {
  288. return "L";
  289. }
  290. else if ((iCnChar >= 49896) && (iCnChar <= 50370))
  291. {
  292. return "M";
  293. }
  294. else if ((iCnChar >= 50371) && (iCnChar <= 50613))
  295. {
  296. return "N";
  297. }
  298. else if ((iCnChar >= 50614) && (iCnChar <= 50621))
  299. {
  300. return "O";
  301. }
  302. else if ((iCnChar >= 50622) && (iCnChar <= 50905))
  303. {
  304. return "P";
  305. }
  306. else if ((iCnChar >= 50906) && (iCnChar <= 51386))
  307. {
  308. return "Q";
  309. }
  310. else if ((iCnChar >= 51387) && (iCnChar <= 51445))
  311. {
  312. return "R";
  313. }
  314. else if ((iCnChar >= 51446) && (iCnChar <= 52217))
  315. {
  316. return "S";
  317. }
  318. else if ((iCnChar >= 52218) && (iCnChar <= 52697))
  319. {
  320. return "T";
  321. }
  322. else if ((iCnChar >= 52698) && (iCnChar <= 52979))
  323. {
  324. return "W";
  325. }
  326. else if ((iCnChar >= 52980) && (iCnChar <= 53640))
  327. {
  328. return "X";
  329. }
  330. else if ((iCnChar >= 53689) && (iCnChar <= 54480))
  331. {
  332. return "Y";
  333. }
  334. else if ((iCnChar >= 54481) && (iCnChar <= 55289))
  335. {
  336. return "Z";
  337. }
  338. else
  339. return ("?");
  340. }
  341. #endregion
  342. #region 获得IP地址
  343. /// <summary>
  344. /// 获得IP地址
  345. /// </summary>
  346. /// <returns>字符串数组</returns>
  347. public static string GetIp()
  348. {
  349. HttpContextAccessor _context = new HttpContextAccessor();
  350. var ip = _context.HttpContext.Request.Headers["X-Forwarded-For"].ToString();
  351. if (string.IsNullOrEmpty(ip))
  352. {
  353. ip = _context.HttpContext.Connection.RemoteIpAddress.ToString();
  354. }
  355. return ip;
  356. }
  357. #endregion
  358. #region 获得当前访问的URL地址
  359. /// <summary>
  360. /// 获得当前访问的URL地址
  361. /// </summary>
  362. /// <returns>字符串数组</returns>
  363. public static string GetUrl()
  364. {
  365. HttpContextAccessor _context = new HttpContextAccessor();
  366. return _context.HttpContext.Request.Path.ToString();
  367. }
  368. #endregion
  369. #region 分割字符串
  370. public static string[] SplitString(string strContent, char strSplit)
  371. {
  372. if (!string.IsNullOrEmpty(strContent))
  373. {
  374. return strContent.Split(new char[] { strSplit }, StringSplitOptions.RemoveEmptyEntries);
  375. }
  376. else
  377. {
  378. return new string[0] { };
  379. }
  380. }
  381. /// <summary>
  382. /// 分割字符串
  383. /// </summary>
  384. public static string[] SplitString(string strContent, string strSplit)
  385. {
  386. if (!string.IsNullOrEmpty(strContent))
  387. {
  388. if (strContent.IndexOf(strSplit, StringComparison.Ordinal) < 0)
  389. return new string[] { strContent };
  390. return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase);
  391. }
  392. else
  393. return new string[0] { };
  394. }
  395. #endregion
  396. #region 显示错层方法
  397. /// <summary>
  398. /// 显示错层方法
  399. /// </summary>
  400. public static string LevelName(string name, decimal? level)
  401. {
  402. if (level > 1)
  403. {
  404. string nbsp = "";
  405. for (int i = 0; i < level; i++)
  406. {
  407. nbsp += " ";
  408. }
  409. name = nbsp + "|--" + name;
  410. }
  411. return name;
  412. }
  413. #endregion
  414. #region 生成随机字母或数字
  415. private static readonly Random Random = new Random();
  416. /// <summary>
  417. /// 生成随机数字
  418. /// </summary>
  419. /// <param name="length">生成长度</param>
  420. /// <returns></returns>
  421. public static string Number(int length)
  422. {
  423. return Number(length, false);
  424. }
  425. /// <summary>
  426. /// 生成随机数字
  427. /// </summary>
  428. /// <param name="length">生成长度</param>
  429. /// <param name="sleep">是否要在生成前将当前线程阻止以避免重复</param>
  430. /// <returns></returns>
  431. public static string Number(int length, bool sleep)
  432. {
  433. if (sleep)
  434. System.Threading.Thread.Sleep(2);
  435. string result = "";
  436. for (int i = 0; i < length; i++)
  437. {
  438. result += Random.Next(10).ToString();
  439. }
  440. return result;
  441. }
  442. /// <summary>
  443. /// 根据日期和随机码生成订单号
  444. /// </summary>
  445. /// <returns></returns>
  446. public static string GetOrderNumber()
  447. {
  448. string num = DateTime.Now.ToString("yyyyMMddHHmmssms"); //yyyyMMddHHmmssms
  449. return num + Number(2);
  450. }
  451. #endregion
  452. #region 条形码解析
  453. /// <summary>
  454. /// 分解条形码,并返回数组
  455. /// </summary>
  456. /// <param name="code">条形码</param>
  457. /// <returns>数组0=品牌 1=季节 2=款式 3=批次 4=尺码</returns>
  458. public static List<string> GetSkuArray(string code)
  459. {
  460. var str = new List<string>();
  461. //品牌,截取0-3
  462. str.Add(code.Substring(0, 3));
  463. //季节截取
  464. str.Add(code.Substring(3, 1));
  465. //款式截取
  466. str.Add(code.Substring(4, 1));
  467. //批次截取
  468. str.Add(code.Substring(5, 1));
  469. //尺码截取
  470. str.Add(code.Substring(6, 1));
  471. return str;
  472. }
  473. #endregion
  474. #region 返回采购单入库状态
  475. /// <summary>
  476. /// 返回采购单入库状态
  477. /// </summary>
  478. /// <param name="status">状态</param>
  479. /// <returns></returns>
  480. public static string PurchaseStatus(string status)
  481. {
  482. var str = "";
  483. switch (int.Parse(status))
  484. {
  485. case 1: str = "未完成入库"; break;
  486. case 2: str = "未完成付款"; break;
  487. case 3: str = "未完成到票"; break;
  488. case 4: str = "完成"; break;
  489. }
  490. return str;
  491. }
  492. #endregion
  493. #region 生成采购单编号
  494. /// <summary>
  495. /// 生成采购单编号
  496. /// </summary>
  497. /// <returns></returns>
  498. public static string PurchaseNumber(int lastNumber)
  499. {
  500. return "CG-" + DateTime.Now.ToString("yyyyMMdd") + "-" + lastNumber.ToString();
  501. }
  502. #endregion
  503. #region 上传配置
  504. /// <summary>
  505. /// 根据文件类型分配路径
  506. /// </summary>
  507. /// <param name="fileExt"></param>
  508. /// <returns></returns>
  509. public static string AssigendPath(string fileExt, string path)
  510. {
  511. if (IsImage(fileExt))
  512. return path + "/upload/images/" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "/";
  513. if (IsVideos(fileExt))
  514. return path + "/upload/videos/" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "/";
  515. if (IsDocument(fileExt))
  516. return "/upload/files/" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "/";
  517. if (IsMusics(fileExt))
  518. return "/upload/musics/" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "/";
  519. return path + "/upload/others/";
  520. }
  521. #endregion
  522. #region 文件格式
  523. /// <summary>
  524. /// 是否为图片
  525. /// </summary>
  526. /// <param name="_fileExt">文件扩展名,不含“.”</param>
  527. /// <returns></returns>
  528. private static bool IsImage(string _fileExt)
  529. {
  530. var images = new List<string> { "bmp", "gif", "jpg", "jpeg", "png" };
  531. if (images.Contains(_fileExt.ToLower())) return true;
  532. return false;
  533. }
  534. /// <summary>
  535. /// 是否为视频
  536. /// </summary>
  537. /// <param name="_fileExt">文件扩展名,不含“.”</param>
  538. /// <returns></returns>
  539. private static bool IsVideos(string _fileExt)
  540. {
  541. var videos = new List<string> { "rmvb", "mkv", "ts", "wma", "avi", "rm", "mp4", "flv", "mpeg", "mov", "3gp", "mpg" };
  542. if (videos.Contains(_fileExt.ToLower())) return true;
  543. return false;
  544. }
  545. /// <summary>
  546. /// 是否为音频
  547. /// </summary>
  548. /// <param name="_fileExt">文件扩展名,不含“.”</param>
  549. /// <returns></returns>
  550. private static bool IsMusics(string _fileExt)
  551. {
  552. var musics = new List<string> { "mp3", "wav" };
  553. if (musics.Contains(_fileExt.ToLower())) return true;
  554. return false;
  555. }
  556. /// <summary>
  557. /// 是否为文档
  558. /// </summary>
  559. /// <param name="_fileExt">文件扩展名,不含“.”</param>
  560. /// <returns></returns>
  561. private static bool IsDocument(string _fileExt)
  562. {
  563. var documents = new List<string> { "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "pdf" };
  564. if (documents.Contains(_fileExt.ToLower())) return true;
  565. return false;
  566. }
  567. #endregion
  568. #region 返回活动名称
  569. /// <summary>
  570. /// 返回活动名称
  571. /// </summary>
  572. /// <param name="method"></param>
  573. /// <returns></returns>
  574. public static string GetActivityMethod(int method)
  575. {
  576. var str = "";
  577. switch (method)
  578. {
  579. case 1: str = "打折"; break;
  580. case 2: str = "满减"; break;
  581. case 3: str = "买一赠一"; break;
  582. default: str = "无"; break;
  583. }
  584. return str;
  585. }
  586. /// <summary>
  587. /// 根据方式返回参加活动类型
  588. /// </summary>
  589. /// <param name="method"></param>
  590. /// <returns></returns>
  591. public static byte GetActivityTypes(int method)
  592. {
  593. byte str = 1;
  594. switch (method)
  595. {
  596. case 1: str = 2; break;
  597. case 2: str = 3; break;
  598. case 3: str = 4; break;
  599. }
  600. return str;
  601. }
  602. #endregion
  603. #region 得到一周的周一和周日的日期
  604. /// <summary>
  605. /// 计算本周的周一日期
  606. /// </summary>
  607. /// <returns></returns>
  608. public static DateTime GetMondayDate()
  609. {
  610. return GetMondayDate(DateTime.Now);
  611. }
  612. /// <summary>
  613. /// 计算本周周日的日期
  614. /// </summary>
  615. /// <returns></returns>
  616. public static DateTime GetSundayDate()
  617. {
  618. return GetSundayDate(DateTime.Now);
  619. }
  620. /// <summary>
  621. /// 计算某日起始日期(礼拜一的日期)
  622. /// </summary>
  623. /// <param name="someDate">该周中任意一天</param>
  624. /// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns>
  625. public static DateTime GetMondayDate(DateTime someDate)
  626. {
  627. int i = someDate.DayOfWeek - DayOfWeek.Monday;
  628. if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。
  629. TimeSpan ts = new TimeSpan(i, 0, 0, 0);
  630. return someDate.Subtract(ts);
  631. }
  632. /// <summary>
  633. /// 计算某日结束日期(礼拜日的日期)
  634. /// </summary>
  635. /// <param name="someDate">该周中任意一天</param>
  636. /// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns>
  637. public static DateTime GetSundayDate(DateTime someDate)
  638. {
  639. int i = someDate.DayOfWeek - DayOfWeek.Sunday;
  640. if (i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。
  641. TimeSpan ts = new TimeSpan(i, 0, 0, 0);
  642. return someDate.Add(ts);
  643. }
  644. /// <summary>
  645. /// 根据星期几获得数字的星期几
  646. /// </summary>
  647. /// <param name="weekName">例如周一:Monday</param>
  648. /// <returns></returns>
  649. public static int GetWeekByWeekName(string weekName)
  650. {
  651. var week = 1;
  652. switch (weekName)
  653. {
  654. case "Monday":
  655. week = 1;
  656. break;
  657. case "Tuesday":
  658. week = 2;
  659. break;
  660. case "Wednesday":
  661. week = 3;
  662. break;
  663. case "Thursday":
  664. week = 4;
  665. break;
  666. case "Friday":
  667. week = 5;
  668. break;
  669. case "Saturday":
  670. week = 6;
  671. break;
  672. case "Sunday":
  673. week = 7;
  674. break;
  675. }
  676. return week;
  677. }
  678. #endregion
  679. /// <summary>
  680. /// 时间戳转换为日期(时间戳单位秒)
  681. /// </summary>
  682. /// <param name="TimeStamp"></param>
  683. /// <returns></returns>
  684. public static DateTime ConvertToDateTime(long timeStamp)
  685. {
  686. var dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  687. TimeSpan toNow = new TimeSpan(timeStamp);
  688. return dtStart.Add(toNow);
  689. //var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  690. //return start.AddMilliseconds(timeStamp).AddHours(8);
  691. }
  692. /// <summary>
  693. /// 日期转换为时间戳(时间戳单位秒)
  694. /// </summary>
  695. /// <param name="TimeStamp"></param>
  696. /// <returns></returns>
  697. public static long ConvertToTimeStamp(DateTime time)
  698. {
  699. DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  700. return (long)(time.AddHours(-8) - Jan1st1970).TotalMilliseconds;
  701. }
  702. }
  703. }