123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- import { conforms } from "lodash";
- export const calcDate = (date1, date2) => {
- let date3 = date2 - date1;
- let days = Math.floor(date3 / (24 * 3600 * 1000))
- let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
- let hours = Math.floor(leave1 / (3600 * 1000))
- let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
- let minutes = Math.floor(leave2 / (60 * 1000))
- let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
- let seconds = Math.round(date3 / 1000)
- return {
- leave1,
- leave2,
- leave3,
- days: days,
- hours: hours,
- minutes: minutes,
- seconds: seconds,
- }
- }
- /**
- * 日期格式化
- */
- export function dateFormat(date, format) {
- format = format || 'yyyy-MM-dd hh:mm:ss';
- if (date !== 'Invalid Date') {
- let o = {
- "M+": date.getMonth() + 1, //month
- "d+": date.getDate(), //day
- "h+": date.getHours(), //hour
- "m+": date.getMinutes(), //minute
- "s+": date.getSeconds(), //second
- "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
- "S": date.getMilliseconds() //millisecond
- }
- if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
- (date.getFullYear() + "").substr(4 - RegExp.$1.length));
- for (let k in o)
- if (new RegExp("(" + k + ")").test(format))
- format = format.replace(RegExp.$1,
- RegExp.$1.length === 1 ? o[k] :
- ("00" + o[k]).substr(("" + o[k]).length));
- return format;
- }
- return '';
- }
- /**
- * 获取上月第一天和下月最后一天
- * type 1为 [yyyy-MM-dd,yyyy-MM-dd]
- * type 2为 [yyyy-MM-dd 00:00:00,yyyy-MM-dd 23:59:59]
- * type 4为 [yyyy-MM-dd,yyyy-MM-dd] 当月的第一天和最后一天
- * type为空默认2
- */
- export function defaultDate(type) {
- type = type ? type : 2
- const date = new Date();
- const startDate = new Date(date.getFullYear(), date.getMonth() - 1, 1);
- const endDate = new Date(date.getFullYear(), date.getMonth() + 2, 0);
- if (type == 1) {
- return [
- dateFormat(startDate, "yyyy-MM-dd"),
- dateFormat(endDate, "yyyy-MM-dd")
- ];
- } else if (type == 3) {
- return [
- dateFormat(new Date(date.getFullYear(), date.getMonth() - 1, 1), "yyyy-MM-dd"),
- dateFormat(new Date((date.getMonth() + 1) === 12 ? date.getFullYear() + 1 : date.getFullYear(), (date.getMonth() + 1), date.getDate()), "yyyy-MM-dd")
- ];
- } else if (type == 4) {
- return [
- dateFormat(new Date(date.getFullYear(), date.getMonth(), 1), "yyyy-MM-dd"),
- dateFormat(new Date(date.getFullYear(), date.getMonth() + 1, 0), "yyyy-MM-dd")
- ];
- } else {
- return [
- dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
- dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
- ];
- }
- }
- /**
- * 获取当天
- * [yyyy-MM-dd,yyyy-MM-dd]
- */
- export function defaultDate2() {
- const date = new Date();
- return [
- dateFormat(date, "yyyy-MM-dd"),
- dateFormat(date, "yyyy-MM-dd")
- ];
- }
- /**
- * 获取本月第一天和本月最后一天
- * [yyyy-MM-dd,yyyy-MM-dd]
- */
- export function defaultDate3() {
- const date = new Date();
- const startDate = new Date(date.getFullYear(), date.getMonth(), 1);
- const endDate = new Date(date.getFullYear(), date.getMonth() + 1, 0);
- return [
- dateFormat(startDate, "yyyy-MM-dd"),
- dateFormat(endDate, "yyyy-MM-dd")
- ];
- }
- /**
- * 获取本月第一天
- * [yyyy-MM-dd,yyyy-MM-dd]
- */
- export function startDate() {
- const date = new Date();
- const startDate = new Date(date.getFullYear(), date.getMonth(), 1);
- return dateFormat(startDate, "yyyy-MM-dd");
- }
- /**
- * 获取本月最后一天
- * [yyyy-MM-dd,yyyy-MM-dd]
- */
- export function endDate() {
- const date = new Date();
- const endDate = new Date(date.getFullYear(), date.getMonth() + 1, 0);
- return dateFormat(endDate, "yyyy-MM-dd");
- }
- /**
- * 获取当年
- * [yyyy-MM-dd,yyyy-MM-dd]
- */
- export function defaultDate4() {
- const date = new Date();
- return dateFormat(date, "yyyy");
- }
- /**
- * 获取本年上个月
- */
- export function CurrentMonth(date, format = 'yyyy-MM-dd hh:mm:ss') {
- format = format || 'yyyy-MM-dd hh:mm:ss';
- if (date !== 'Invalid Date') {
- let o = {
- "M+": date.getMonth(), //month
- "d+": date.getDate(), //day
- "h+": date.getHours(), //hour
- "m+": date.getMinutes(), //minute
- "s+": date.getSeconds(), //second
- "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
- "S": date.getMilliseconds() //millisecond
- }
- if (Number(o["M+"]) == 0) {
- o["M+"] = 1
- }
- if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
- (date.getFullYear() + "").substr(4 - RegExp.$1.length));
- for (let k in o) {
- if (new RegExp("(" + k + ")").test(format)) {
- format = format.replace(RegExp.$1,
- RegExp.$1.length === 1 ? o[k] :
- ("00" + o[k]).substr(("" + o[k]).length));
- }
- }
- return format;
- }
- return '';
- }
- /**
- * 获取上月第一天和上月最后一天
- * [yyyy-MM-dd,yyyy-MM-dd]
- */
- export function defaultDate5() {
- const date = new Date();
- const startDate = new Date(date.getFullYear(), date.getMonth() - 1, 1);
- const endDate = new Date(date.getFullYear(), date.getMonth(), 0);
- return [
- dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
- dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
- ];
- }
- /**
- * 获取本月第一天和本月最后一天
- * [yyyy-MM-dd,yyyy-MM-dd]
- */
- export function defaultDate6() {
- const date = new Date();
- const startDate = new Date(date.getFullYear(), date.getMonth(), 1);
- const endDate = new Date(date.getFullYear(), date.getMonth() + 1, 0);
- return [
- dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
- dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
- ];
- }
- // 获得当前日期
- export function getCurrentDate(type = 'dateTime') {
- const date = new Date();
- const currentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
- if (type == 'date') {
- return dateFormat(currentDate, 'yyyy-MM-dd')
- } else if (type == 'dateTime') {
- return dateFormat(date, 'yyyy-MM-dd hh:mm:ss')
- }
- }
- export function getYearDate() {
- const date = new Date();
- return date.getFullYear()
- }
- export function getMonthDate() {
- const date = new Date();
- return date.getMonth()+1
- }
- /**
- * 获取 n 天后的日期
- * yyyy-MM-dd
- */
- export const NdayDate = (num) => {
- let date1 = new Date();
- //今天时间
- let time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
- let date2 = new Date(date1);
- date2.setDate(date1.getDate() + num);
- //num是正数表示之后的时间,num负数表示之前的时间,0表示今天
- let time2 = addZero(date2.getFullYear()) + "-" + addZero((date2.getMonth() + 1)) + "-" + addZero(date2.getDate());
- return time2;
- }
- // 补零方法
- function addZero(num) {
- if (parseInt(num) < 10) {
- num = '0' + num;
- }
- return num;
- }
- //验证英文
- export function verifyEnglish(str) {
- const regex = /^[A-Za-z0-9\r\n .,!?:()|/+-_;'"$&@%*#]+$/;
- return regex.test(str ? str : null);
- }
- //验证符号
- export function verifySymbol(str) {
- const regex = /^[A-Za-z0-9\r\n .,!?:()|/+-_;'"$&@%*#]+$/;
- return regex.test(str ? str : null);
- }
- // 匹配常见全角符号的正则表达式(包括标点、字母、数字等)
- export function checkFullWidthSymbols(text) {
- if (text) {
- const fullWidthRegex = /[\u3000-\u303F\uFF01-\uFF5E\uFFE0-\uFFE5]/g;
- const matchesItem = [];
- let lines = text.split('\n')
- let match;
- lines.forEach((ln, index, array) => {
- while ((match = fullWidthRegex.exec(ln)) !== null) {
- matchesItem.push({
- row: index + 1, // 行号
- index: match.index, // 符号的位置
- symbol: match[0] // 符号本身
- });
- }
- })
- let matches = [];
- matches = matchesItem.reduce((acc, item) => {
- const existingItem = acc.find(i => i.row === item.row);
- if (existingItem) {
- existingItem.symbol += ` ${item.symbol}`; // 合并值
- } else {
- acc.push({ ...item }); // 添加新项
- }
- return acc;
- }, []);
- return {
- hasFullWidth: matches.length > 0, // 是否包含全角符号
- positions: matches // 符号的详细位置和内容
- };
- } else {
- return {
- hasFullWidth: false, // 是否包含全角符号
- positions: [] // 符号的详细位置和内容
- };
- }
- }
|