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 "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;
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取当前月份第一天最后一天时间戳,可以使用 JavaScript 的 Date 对象,结合 `getTime()` 方法。具体的代码如下所示: ```javascript const now = new Date(); // 获取当前时间 const year = now.getFullYear(); // 年份 const month = now.getMonth() + 1; // 月份,注意要加 1 const firstDay = new Date(year, month - 1, 1); // 当前月份第一天 const lastDay = new Date(year, month, 0); // 当前月份最后一天 const firstDayTimestamp = firstDay.getTime(); // 当前月份第一天时间戳 const lastDayTimestamp = lastDay.getTime(); // 当前月份最后一天时间戳 console.log(`当前月份第一天时间戳:${firstDayTimestamp}`); // 输出:当前月份第一天时间戳:1633046400000 console.log(`当前月份最后一天时间戳:${lastDayTimestamp}`); // 输出:当前月份最后一天时间戳:1635638399999 ``` 在上面的代码中,我们首先使用 `new Date()` 获取当前时间,然后通过 `getFullYear()` 和 `getMonth()` 方法获取当前的年份月份。接着,我们使用 `new Date(year, month - 1, 1)` 创建了一个新的 Date 对象,其中 `year` 和 `month - 1` 分别表示年份月份,`1` 表示天数,即当前月份第一天。同样地,我们使用 `new Date(year, month, 0)` 创建了另一个 Date 对象,其中 `year` 和 `month` 分别表示年份月份,`0` 表示天数,即当前月份最后一天最后,我们使用 `getTime()` 方法获取了当前月份第一天最后一天时间戳,并输出到控制台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值