date.js 7.6 KB

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