js 获取指定日期,上一月及下一月(验证日期的格式)

  • Post author:
  • Post category:其他


/**

* 获取指定日期的上一个月

*

* @date 验证日期的格式

*/

// 获取指定日期的上一个月
function getPreMonth(date) {
  //判断日期常用格式的正则
  let regA = /^(\d{4})-(\d{1,2})-(\d{1,2})$/, //日期格式为:yyyy-mm-dd
    regB = /^(\d{4})年(\d{1,2})月(\d{1,2})日$/, //日期格式为:yyyy年mm月dd日
    regC = /^(\d{4})年-(\d{1,2})月-(\d{1,2})日$/, //日期格式为:yyyy年-mm月-dd日
    regD = /^(\d{4})(\d{2})(\d{2})$/, //日期格式为:yyyymmdd
    regE = /^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/, //日期格式为:yyyy-mm-dd hh:mm:ss
    regF = /^(\d{4})年(\d{1,2})月(\d{1,2})日 (\d{1,2}):(\d{1,2}):(\d{1,2})$/, //日期格式为:yyyy年mm月dd日 hh:mm:ss
    regG = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/; //日期格式为:yyyymmddhhmmss
  //判断传入的日期进行正则表达式匹配
  let arr = [];
  switch (true) {
    case regA.test(date):
    case regB.test(date):
    case regC.test(date):
    case regD.test(date):
      arr.push(RegExp.$1, RegExp.$2, RegExp.$3); //RegExp.$1获取匹配到的第一个值匹配,即‘2022’,以此类推
      console.log(arr, "arr");
      break;
    case regE.test(date):
    case regF.test(date):
    case regG.test(date):
      arr.push(
        RegExp.$1,
        RegExp.$2,
        RegExp.$3,
        RegExp.$4,
        RegExp.$5,
        RegExp.$6
      );
      console.log(arr, "arr");
      break;

    default:
      console.log("没有满足的日期格式");
      return;
  }

  arr = [...arr];
  let [year, month, day, hour, minute, second] = arr;

  let newYear = year,
    newDay = day,
    newMonth = parseInt(month) - 1;
  if (newMonth == 0) {
    newYear = parseInt(newYear) - 1;
    newMonth = 12;
  }
  let lastDays = new Date(newYear, newMonth, 0);
  lastDays = lastDays.getDate();

  newDay = newDay > lastDays ? lastDays : newDay;

  newMonth =
    newMonth < 10 && String(newMonth).length < 2 ? "0" + newMonth : newMonth;
  newDay = newDay < 10 && newDay.length < 2 ? "0" + newDay : newDay;
  minute = minute < 10 && minute.length < 2 ? "0" + minute : minute;
  second = second < 10 && second.length < 2 ? "0" + second : second;

  return arr.length > 3
    ? `${newYear}-${newMonth}-${newDay} ${hour}:${minute}:${second}`
    : `${newYear}-${newMonth}-${newDay}`;
}

代码中用到了 switch case加范围判断,switch case是可以加范围判断的,但是语法有少许变化,参数不能写在switch里面,而是写在外面。

测试结果

不带时间格式

console.log(getPreMonth('2022-07-11')); //2022-06-11 

带时间格式

console.log(this.getPreMonth('2022-07-11 12:23:12')); //2022-06-11 12:23:12 

/**

* 获取指定日期的下一个月 (这里用了另一种方法验证日期)

*

* @date 验证日期的格式

*/

function getNextMonth(date) {
  //判断日期常用格式的正则
  let regObj = {
      regA: /^(\d{4})-(\d{1,2})-(\d{1,2})$/, //日期格式为:yyyy-mm-dd
      regB: /^(\d{4})年(\d{1,2})月(\d{1,2})日$/, //日期格式为:yyyy年mm月dd日
      regC: /^(\d{4})年-(\d{1,2})月-(\d{1,2})日$/, //日期格式为:yyyy年-mm月-dd日
      regD: /^(\d{4})(\d{2})(\d{2})$/, //日期格式为:yyyymmdd
    },
    regObj2 = {
      regE: /^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/, //日期格式为:yyyy-mm-dd hh:mm:ss
      regF: /^(\d{4})年(\d{1,2})月(\d{1,2})日 (\d{1,2}):(\d{1,2}):(\d{1,2})$/, //日期格式为:yyyy年mm月dd日 hh:mm:ss
      regG: /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/, //日期格式为:yyyymmddhhmmss
    };

  //判断传入的日期进行正则表达式匹配
  let arr = [],
    valArr = [];
  //调用日期正则匹配公共方法
  if (dateRegexFun(date, regObj)) {
    valArr = dateRegexFun(date, regObj);
    arr.push(valArr[1], valArr[2], valArr[3]);
  }
  //调用日期正则匹配公共方法
  if (dateRegexFun(date, regObj2)) {
    valArr = dateRegexFun(date, regObj2);
    arr.push(valArr[1], valArr[2], valArr[3], valArr[4], valArr[5], valArr[6]);
  }

  if (arr.length === 0) return;
  arr = [...arr];
  let [year, month, day, hour, minute, second] = arr;

  let newYear = year,
    newDay = day,
    newMonth = parseInt(month) + 1;
  if (newMonth == 13) {
    newYear = parseInt(newYear) + 1;
    newMonth = 1;
  }
  let nextDays = new Date(newYear, newMonth, 0);
  nextDays = nextDays.getDate();

  newDay = newDay > nextDays ? nextDays : newDay;
  newMonth =
    newMonth < 10 && String(newMonth).length < 2 ? "0" + newMonth : newMonth;
  newDay = newDay < 10 && newDay.length < 2 ? "0" + newDay : newDay;
  minute = minute < 10 && minute.length < 2 ? "0" + minute : minute;
  second = second < 10 && second.length < 2 ? "0" + second : second;

  return arr.length > 3
    ? `${newYear}-${newMonth}-${newDay} ${hour}:${minute}:${second}`
    : `${newYear}-${newMonth}-${newDay}`;
}

//日期正则匹配公共方法

function dateRegexFun(date, obj) {
  for (let key in obj) {
    // console.log(date.match(obj[key]))
    if (date.match(obj[key])) return date.match(obj[key]);
  }
}

测试结果

不带时间格式

console.log(getNextMonth('2022-07-11')); //2022-08-11 

带时间格式

console.log(getNextMonth('2022-07-11 12:23:12')); // 2022-08-11 12:23:12 main.js

到此结束。



版权声明:本文为weixin_43743175原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。