date.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { conforms } from "lodash";
  2. export const calcDate = (date1, date2) => {
  3. let date3 = date2 - date1;
  4. let days = Math.floor(date3 / (24 * 3600 * 1000))
  5. let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
  6. let hours = Math.floor(leave1 / (3600 * 1000))
  7. let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
  8. let minutes = Math.floor(leave2 / (60 * 1000))
  9. let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
  10. let seconds = Math.round(date3 / 1000)
  11. return {
  12. leave1,
  13. leave2,
  14. leave3,
  15. days: days,
  16. hours: hours,
  17. minutes: minutes,
  18. seconds: seconds,
  19. }
  20. }
  21. /**
  22. * 日期格式化
  23. */
  24. export function dateFormat(date, format) {
  25. format = format || 'yyyy-MM-dd hh:mm:ss';
  26. if (date !== 'Invalid Date') {
  27. let o = {
  28. "M+": date.getMonth() + 1, //month
  29. "d+": date.getDate(), //day
  30. "h+": date.getHours(), //hour
  31. "m+": date.getMinutes(), //minute
  32. "s+": date.getSeconds(), //second
  33. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  34. "S": date.getMilliseconds() //millisecond
  35. }
  36. if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
  37. (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  38. for (let k in o)
  39. if (new RegExp("(" + k + ")").test(format))
  40. format = format.replace(RegExp.$1,
  41. RegExp.$1.length === 1 ? o[k] :
  42. ("00" + o[k]).substr(("" + o[k]).length));
  43. return format;
  44. }
  45. return '';
  46. }
  47. /**
  48. * 获取上月第一天和下月最后一天
  49. * type 1为 [yyyy-MM-dd,yyyy-MM-dd]
  50. * type 2为 [yyyy-MM-dd 00:00:00,yyyy-MM-dd 23:59:59]
  51. * type 4为 [yyyy-MM-dd,yyyy-MM-dd] 当月的第一天和最后一天
  52. * type为空默认2
  53. */
  54. export function defaultDate(type) {
  55. type = type ? type : 2
  56. const date = new Date();
  57. const startDate = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  58. const endDate = new Date(date.getFullYear(), date.getMonth() + 2, 0);
  59. if (type == 1) {
  60. return [
  61. dateFormat(startDate, "yyyy-MM-dd"),
  62. dateFormat(endDate, "yyyy-MM-dd")
  63. ];
  64. } else if (type == 3) {
  65. return [
  66. dateFormat(new Date(date.getFullYear(), date.getMonth() - 1, 1), "yyyy-MM-dd"),
  67. dateFormat(new Date((date.getMonth() + 1) === 12 ? date.getFullYear() + 1 : date.getFullYear(), (date.getMonth() + 1), date.getDate()), "yyyy-MM-dd")
  68. ];
  69. } else if (type == 4) {
  70. return [
  71. dateFormat(new Date(date.getFullYear(), date.getMonth(), 1), "yyyy-MM-dd"),
  72. dateFormat(new Date(date.getFullYear(), date.getMonth() + 1, 0), "yyyy-MM-dd")
  73. ];
  74. } else {
  75. return [
  76. dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
  77. dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
  78. ];
  79. }
  80. }
  81. /**
  82. * 获取当天
  83. * [yyyy-MM-dd,yyyy-MM-dd]
  84. */
  85. export function defaultDate2() {
  86. const date = new Date();
  87. return [
  88. dateFormat(date, "yyyy-MM-dd"),
  89. dateFormat(date, "yyyy-MM-dd")
  90. ];
  91. }
  92. /**
  93. * 获取本月第一天和本月最后一天
  94. * [yyyy-MM-dd,yyyy-MM-dd]
  95. */
  96. export function defaultDate3() {
  97. const date = new Date();
  98. const startDate = new Date(date.getFullYear(), date.getMonth(), 1);
  99. const endDate = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  100. return [
  101. dateFormat(startDate, "yyyy-MM-dd"),
  102. dateFormat(endDate, "yyyy-MM-dd")
  103. ];
  104. }
  105. /**
  106. * 获取本月第一天
  107. * [yyyy-MM-dd,yyyy-MM-dd]
  108. */
  109. export function startDate() {
  110. const date = new Date();
  111. const startDate = new Date(date.getFullYear(), date.getMonth(), 1);
  112. return dateFormat(startDate, "yyyy-MM-dd");
  113. }
  114. /**
  115. * 获取本月最后一天
  116. * [yyyy-MM-dd,yyyy-MM-dd]
  117. */
  118. export function endDate() {
  119. const date = new Date();
  120. const endDate = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  121. return dateFormat(endDate, "yyyy-MM-dd");
  122. }
  123. /**
  124. * 获取当年
  125. * [yyyy-MM-dd,yyyy-MM-dd]
  126. */
  127. export function defaultDate4() {
  128. const date = new Date();
  129. return dateFormat(date, "yyyy");
  130. }
  131. /**
  132. * 获取本年上个月
  133. */
  134. export function CurrentMonth(date, format = 'yyyy-MM-dd hh:mm:ss') {
  135. format = format || 'yyyy-MM-dd hh:mm:ss';
  136. if (date !== 'Invalid Date') {
  137. let o = {
  138. "M+": date.getMonth(), //month
  139. "d+": date.getDate(), //day
  140. "h+": date.getHours(), //hour
  141. "m+": date.getMinutes(), //minute
  142. "s+": date.getSeconds(), //second
  143. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  144. "S": date.getMilliseconds() //millisecond
  145. }
  146. if (Number(o["M+"]) == 0) {
  147. o["M+"] = 1
  148. }
  149. if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
  150. (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  151. for (let k in o) {
  152. if (new RegExp("(" + k + ")").test(format)) {
  153. format = format.replace(RegExp.$1,
  154. RegExp.$1.length === 1 ? o[k] :
  155. ("00" + o[k]).substr(("" + o[k]).length));
  156. }
  157. }
  158. return format;
  159. }
  160. return '';
  161. }
  162. /**
  163. * 获取上月第一天和上月最后一天
  164. * [yyyy-MM-dd,yyyy-MM-dd]
  165. */
  166. export function defaultDate5() {
  167. const date = new Date();
  168. const startDate = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  169. const endDate = new Date(date.getFullYear(), date.getMonth(), 0);
  170. return [
  171. dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
  172. dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
  173. ];
  174. }
  175. /**
  176. * 获取本月第一天和本月最后一天
  177. * [yyyy-MM-dd,yyyy-MM-dd]
  178. */
  179. export function defaultDate6() {
  180. const date = new Date();
  181. const startDate = new Date(date.getFullYear(), date.getMonth(), 1);
  182. const endDate = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  183. return [
  184. dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
  185. dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
  186. ];
  187. }
  188. // 获得当前日期
  189. export function getCurrentDate(type = 'dateTime') {
  190. const date = new Date();
  191. const currentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  192. if (type == 'date') {
  193. return dateFormat(currentDate, 'yyyy-MM-dd')
  194. } else if (type == 'dateTime') {
  195. return dateFormat(date, 'yyyy-MM-dd hh:mm:ss')
  196. }
  197. }
  198. export function getYearDate() {
  199. const date = new Date();
  200. return date.getFullYear()
  201. }
  202. export function getMonthDate() {
  203. const date = new Date();
  204. return date.getMonth()+1
  205. }
  206. /**
  207. * 获取 n 天后的日期
  208. * yyyy-MM-dd
  209. */
  210. export const NdayDate = (num) => {
  211. let date1 = new Date();
  212. //今天时间
  213. let time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
  214. let date2 = new Date(date1);
  215. date2.setDate(date1.getDate() + num);
  216. //num是正数表示之后的时间,num负数表示之前的时间,0表示今天
  217. let time2 = addZero(date2.getFullYear()) + "-" + addZero((date2.getMonth() + 1)) + "-" + addZero(date2.getDate());
  218. return time2;
  219. }
  220. // 补零方法
  221. function addZero(num) {
  222. if (parseInt(num) < 10) {
  223. num = '0' + num;
  224. }
  225. return num;
  226. }
  227. //验证英文
  228. export function verifyEnglish(str) {
  229. const regex = /^[A-Za-z0-9\r\n .,!?:()|/+-_;'"$&@%*#]+$/;
  230. return regex.test(str ? str : null);
  231. }
  232. //验证符号
  233. export function verifySymbol(str) {
  234. const regex = /^[A-Za-z0-9\r\n .,!?:()|/+-_;'"$&@%*#]+$/;
  235. return regex.test(str ? str : null);
  236. }
  237. // 匹配常见全角符号的正则表达式(包括标点、字母、数字等)
  238. export function checkFullWidthSymbols(text) {
  239. if (text) {
  240. const fullWidthRegex = /[\u3000-\u303F\uFF01-\uFF5E\uFFE0-\uFFE5]/g;
  241. const matchesItem = [];
  242. let lines = text.split('\n')
  243. let match;
  244. lines.forEach((ln, index, array) => {
  245. while ((match = fullWidthRegex.exec(ln)) !== null) {
  246. matchesItem.push({
  247. row: index + 1, // 行号
  248. index: match.index, // 符号的位置
  249. symbol: match[0] // 符号本身
  250. });
  251. }
  252. })
  253. let matches = [];
  254. matches = matchesItem.reduce((acc, item) => {
  255. const existingItem = acc.find(i => i.row === item.row);
  256. if (existingItem) {
  257. existingItem.symbol += ` ${item.symbol}`; // 合并值
  258. } else {
  259. acc.push({ ...item }); // 添加新项
  260. }
  261. return acc;
  262. }, []);
  263. return {
  264. hasFullWidth: matches.length > 0, // 是否包含全角符号
  265. positions: matches // 符号的详细位置和内容
  266. };
  267. } else {
  268. return {
  269. hasFullWidth: false, // 是否包含全角符号
  270. positions: [] // 符号的详细位置和内容
  271. };
  272. }
  273. }