锐腾搅拌上料功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
7.2 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ICSSoft.Frame.Helper
  6. {
  7. public class FormatHelper
  8. {
  9. public const char TRUE = '1';
  10. public const char FALSE = '0';
  11. public const string TRUE_STRING = "1";
  12. public const string FALSE_STRING = "0";
  13. /// <summary>
  14. /// 格式化数据库时间
  15. /// </summary>
  16. /// <param name="time"></param>
  17. /// <returns></returns>
  18. public static string ToTimeString(int time)
  19. {
  20. return ToTimeString(time, ":");
  21. }
  22. /// <summary>
  23. /// 只显示时分,不显示秒
  24. /// </summary>
  25. /// <param name="time"></param>
  26. /// <returns></returns>
  27. public static string ToShortTimeString(int time)
  28. {
  29. string timeString = time.ToString().PadLeft(6, '0');
  30. string timeSplitChar = ":";
  31. return string.Format("{0}{1}{2}"
  32. , timeString.Substring(0, 2)
  33. , timeSplitChar
  34. , timeString.Substring(2, 2));
  35. }
  36. /// <summary>
  37. /// 格式化数据库时间
  38. /// </summary>
  39. /// <param name="time"></param>
  40. /// <returns></returns>
  41. public static string ToTimeString(int time, string timeSplitChar)
  42. {
  43. string timeString = time.ToString().PadLeft(6, '0');
  44. return string.Format("{0}{1}{2}{3}{4}"
  45. , timeString.Substring(0, 2)
  46. , timeSplitChar
  47. , timeString.Substring(2, 2)
  48. , timeSplitChar
  49. , timeString.Substring(4, 2));
  50. }
  51. public static int TOTimeInt(System.DateTime dateTime)
  52. {
  53. if (dateTime <= DateTime.MinValue)
  54. {
  55. return 0;
  56. }
  57. return dateTime.Hour * 10000 + dateTime.Minute * 100 + dateTime.Second;
  58. }
  59. public static int TOTimeInt(string time)
  60. {
  61. if (time == null || time.Trim() == string.Empty)
  62. {
  63. return 0;
  64. }
  65. char[] split = new char[] { ':' };
  66. string[] array = time.Split(split);
  67. return System.Int32.Parse(array[0]) * 10000 + System.Int32.Parse(array[1]) * 100 + System.Int32.Parse(array[2]);
  68. }
  69. public static int TODateInt(System.DateTime dateTime)
  70. {
  71. if (dateTime <= DateTime.MinValue)
  72. {
  73. return 0;
  74. }
  75. return dateTime.Year * 10000 + dateTime.Month * 100 + dateTime.Day;
  76. }
  77. public static int TODateInt(string Date)
  78. {
  79. if (Date == null || Date.Trim() == string.Empty)
  80. {
  81. return 0;
  82. }
  83. char[] split = new char[2];
  84. split[0] = '/';
  85. split[1] = '-';
  86. string[] array = null;
  87. array = Date.Split(split);
  88. return System.Int32.Parse(array[0]) * 10000 + System.Int32.Parse(array[1]) * 100 + System.Int32.Parse(array[2]);
  89. }
  90. /// <summary>
  91. /// 获取一年第几周
  92. /// </summary>
  93. /// <param name="time"></param>
  94. /// <returns></returns>
  95. public static int GetRecentWeekOfYear()
  96. {
  97. int weekOfYear = 0;
  98. int days = DateTime.Now.DayOfYear;
  99. int dayofweek = 0;
  100. switch (DateTime.Now.DayOfWeek)
  101. {
  102. case DayOfWeek.Sunday:
  103. dayofweek = 1;
  104. break;
  105. case DayOfWeek.Monday:
  106. dayofweek = 2;
  107. break;
  108. case DayOfWeek.Tuesday:
  109. dayofweek = 3;
  110. break;
  111. case DayOfWeek.Wednesday:
  112. dayofweek = 4;
  113. break;
  114. case DayOfWeek.Thursday:
  115. dayofweek = 5;
  116. break;
  117. case DayOfWeek.Friday:
  118. dayofweek = 6;
  119. break;
  120. case DayOfWeek.Saturday:
  121. dayofweek = 7;
  122. break;
  123. default:
  124. dayofweek = 0;
  125. break;
  126. }
  127. weekOfYear = (days - dayofweek) / 7 + 1;
  128. return weekOfYear;
  129. }
  130. /// <summary>
  131. /// 获取一年第几周
  132. /// </summary>
  133. /// <param name="time"></param>
  134. /// <returns></returns>
  135. public static int GetRecentWeekOfYear(DateTime dt)
  136. {
  137. int weekOfYear = 0;
  138. int days = dt.DayOfYear;
  139. int dayofweek = 0;
  140. switch (DateTime.Now.DayOfWeek)
  141. {
  142. case DayOfWeek.Sunday:
  143. dayofweek = 1;
  144. break;
  145. case DayOfWeek.Monday:
  146. dayofweek = 2;
  147. break;
  148. case DayOfWeek.Tuesday:
  149. dayofweek = 3;
  150. break;
  151. case DayOfWeek.Wednesday:
  152. dayofweek = 4;
  153. break;
  154. case DayOfWeek.Thursday:
  155. dayofweek = 5;
  156. break;
  157. case DayOfWeek.Friday:
  158. dayofweek = 6;
  159. break;
  160. case DayOfWeek.Saturday:
  161. dayofweek = 7;
  162. break;
  163. default:
  164. dayofweek = 0;
  165. break;
  166. }
  167. weekOfYear = (days - dayofweek) / 7 + 1;
  168. return weekOfYear;
  169. }
  170. public static bool StringToBoolean(string value)
  171. {
  172. return value == TRUE_STRING;
  173. }
  174. /// <summary>
  175. /// 返回在指定字符串位置上为1,则返回true,0返回false
  176. /// </summary>
  177. /// <param name="orginalString">指定字符串</param>
  178. /// <param name="index">位置索引</param>
  179. /// <returns></returns>
  180. public static bool StringToBoolean(string orginalString, int index)
  181. {
  182. if ((index > orginalString.Length) || (index < 0))
  183. {
  184. throw new Exception("$Error_Index_Out_Of_Range");
  185. }
  186. return StringToBoolean(orginalString[index].ToString());
  187. }
  188. public static bool StringToBoolean(string value, string trueValue)
  189. {
  190. return value == trueValue;
  191. }
  192. public static string BooleanToString(bool value)
  193. {
  194. return value ? TRUE_STRING : FALSE_STRING;
  195. }
  196. public static string BooleanToGirdCheckBoxString(char value)
  197. {
  198. return value == TRUE ? "true" : "false";
  199. }
  200. public static string BooleanToString(string orginalString, int index, bool value)
  201. {
  202. if ((index > orginalString.Length) || (index < 0))
  203. {
  204. throw new Exception("$Error_Index_Out_Of_Range");
  205. }
  206. char[] tmpChar = orginalString.ToCharArray();
  207. tmpChar[index] = value ? TRUE : FALSE;
  208. return new string(tmpChar);
  209. }
  210. }
  211. }