JS快速获取日期时间(昨天今天明天后天、上下月份、年份的第一天和最后一天)

获取当前日期或任意日期的昨天今天明天后天、上下月份、年份的第一天和最后一天
在这里插入图片描述

   // 这里模拟的当天日期是2008-08-08
    console.log("昨天:" + this.getDateTime(-1));
    console.log("今天:" + this.getDateTime(0));
    console.log("明天:" + this.getDateTime(1));
    console.log("后天:" + this.getDateTime(2));

    console.log("指定日期的第二天(不补0):" + this.getDateTime(1, "yyyy-M-d", "2008-06-06"));
    console.log("今天日期+时间:(修改分隔符)" + this.getDateTime(0, "yyyy/MM/dd HH-mm-ss"));
    console.log("今天日期+时间:" + this.getDateTime(0, "yyyy-MM-dd HH:mm"));
    console.log("今天日期(不包含年):" + this.getDateTime(0, "MM-dd"));
    console.log("今天日期+时间(包含秒):" + this.getDateTime(0, "yyyy-MM-dd HH:mm:ss"));
    console.log("今天日期+时间(包含星期):" + this.getDateTime(0, "yyyy-MM-dd HH:mm:ss week"));

    console.log("当月第一天:" + this.getDateTime("month"));
    console.log("当月最后一天:" + this.getDateTime("monthEnd"));

    console.log("上个月第一天:" + this.getDateTime("beforeMonth"));
    console.log("上个月最后一天:" + this.getDateTime("beforeMonthEnd"));

    console.log("下个月第一天:" + this.getDateTime("afterMonth"));
    console.log("下个月最后一天:" + this.getDateTime("afterMonthEnd"));

    console.log("当年第一天:" + this.getDateTime("year"));
    console.log("当年最后一天:" + this.getDateTime("yearEnd"));

    console.log("上年第一天:" + this.getDateTime("beforeYear"));
    console.log("上年最后一天:" + this.getDateTime("beforeYearEnd"));

    console.log("下年第一天:" + this.getDateTime("afterYear"));
    console.log("下年最后一天:" + this.getDateTime("afterYearEnd"));
       /**
     * @description:
     * @param {*} type 类型 默认今天
     * @param {*} config 格式 yyyy-MM-dd HH:mm:ss week 年-月-日 时:分:秒 星期
     * @param {*} setDate 指定日期
     * @return {*}
     */
    getDateTime(type = 0, config = "yyyy-MM-dd", setDate) {
      let sep = config.match(/[^a-zA-Z0-9]/g); //获取字符串中的符号(即非字母和非数字的字符)
      sep = Array.from(new Set(sep)); //去重

      let strLenObj = [...config].reduce((a, b) => (a[b] ? a[b]++ : (a[b] = 1), a), {});

      let thisDate = new Date();
      if (setDate) thisDate = new Date(setDate); //设置指定日期
      switch (type) {
        case "month": //当月第一天
          thisDate.setDate(1);
          break;
        case "monthEnd": //当月最后一天
          thisDate = new Date(thisDate.getFullYear(), thisDate.getMonth() + 1, 0);
          break;
           case "lastMonth": //上个月当天日期
      // 当前年月的日
      let nowDate = thisDate.getDate();
      let lastMonth = new Date(thisDate.getTime());
      lastMonth.setMonth(lastMonth.getMonth());
      lastMonth.setDate(0);
      let daysOflastMonth = lastMonth.getDate();
      lastMonth.setDate(nowDate > daysOflastMonth ? daysOflastMonth : nowDate);
      thisDate = lastMonth
      break;
        case "beforeMonth": //上个月第一天
          thisDate = new Date(thisDate.getFullYear(), thisDate.getMonth() - 1, 1);
          break;
        case "beforeMonthEnd": //上个月最后一天
          thisDate = new Date(new Date(thisDate.getFullYear(), thisDate.getMonth(), 1) - 1);
          break;
        case "afterMonth": //下个月第一天
          thisDate = new Date(thisDate.getFullYear(), thisDate.getMonth() + 1, 1);
          break;
        case "afterMonthEnd": //下个月最后一天
          thisDate = new Date(thisDate.getFullYear(), thisDate.getMonth() + 2, 0);
          break;

        case "year":
          thisDate.setDate(1); //当年第一天
          thisDate.setMonth(0);
          break;
        case "yearEnd": //当年最后一天
          thisDate.setMonth(12);
          thisDate.setDate(0);
          break;
        case "beforeYear": //上年第一天
          thisDate = new Date(thisDate.getFullYear() - 1, 0, 1);
          break;
        case "beforeYearEnd": //上年最后一天
          thisDate = new Date(thisDate.getFullYear(), 0, 0);
          break;
        case "afterYear": //下年第一天
          thisDate = new Date(thisDate.getFullYear() + 1, 0, 1);
          break;
        case "afterYearEnd": //下年最后一天
          thisDate = new Date(thisDate.getFullYear() + 2, 0, 0);
          break;

        default:
          thisDate.setDate(thisDate.getDate() + type);
          break;
      }

      const year = thisDate.getFullYear();
      const month = thisDate.getMonth() + 1;
      const day = thisDate.getDate();
      const hours = new Date().getHours();
      const minutes = new Date().getMinutes();
      const seconds = new Date().getSeconds();
      // 获取星期
      const weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
      const dayOfWeek = weekdays[thisDate.getDay()];
      let result = "";

      let hasYear = typeof type == "string" && type.toLowerCase().includes("year"); //如果是字符串 判断是佛有年year
      if (strLenObj.y || hasYear) {
        result += year;
      }

      if (strLenObj.M) {
        result += (strLenObj.y || hasYear ? sep[0] : "") + (month < 10 && strLenObj.M > 1 ? "0" + month : month);
      }
      if (strLenObj.d) {
        result += (strLenObj.M ? sep[0] : "") + (day < 10 && strLenObj.d > 1 ? "0" + day : day);
      }

      if (strLenObj.H) result += sep[1] + (hours < 10 && strLenObj.H > 1 ? "0" + hours : hours);
      if (strLenObj.m) result += sep[2] + (minutes < 10 && strLenObj.m > 1 ? "0" + minutes : minutes);
      if (strLenObj.s) result += sep[2] + (seconds < 10 && strLenObj.s > 1 ? "0" + seconds : seconds);

      if (config.toLowerCase().includes("week")) result += sep[1] + dayOfWeek; //包含星期

      return result;
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值