js获取不同时间段

前端常见的时间格式

const time = new Date();
console.log(time);                       // Fri Jun 02 2023 09:08:07 GMT+0800 (中国标准时间)
console.log(time.toDateString());        // Fri Jun 02 2023
console.log(time.toTimeString());        // 12:00:00 GMT+0800 (中国标准时间)
console.log(time.toJSON());              // 2023-01-01T00:00:00.648Z
console.log(time.toISOString());         // 2023-06-01T00:00:00.648Z
console.log(time.toLocaleDateString());  // 2023/6/1
console.log(time.toLocaleString());      // 2023/6/2 09:09:20
console.log(time.toLocaleTimeString());  // 09:08:55
console.log(time.toString());            // Thu Jan 13 2022 10:15:19 GMT+0800 (中国标准时间)
console.log(time.getTime());             // 1642040119648

设置特定时间

const date = new Date();
date.setFullYear(2030)        // 设置年份
date.setMonth(0)			  // 0是一月
date.setDate(10)		   	  // 超过30月份会自动折算date.setDate(31)
date.setHours(10)             // 设置小时
date.setMinutes(20)           // 设置分
date.setSeconds(20)			  // 设置秒(0-59秒)
date.setMilliseconds(999)	  // 设置毫秒(0-999)
date.setTime(1685030400000)	  // 时间戳(精确到毫秒)

获取各时间段

    // 格式化时间函数
    function formatDateTime(date) {
      function pad(num) {
        return num.toString().padStart(2, "0");
      }
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const day = date.getDate();
      return year + "-" + pad(month) + "-" + pad(day);
    }

    // ==================== 今天至明天 ====================
    function getCurrentDay() {
      const date = new Date();
      const dayStart = formatDateTime(date);
      // 明天
      date.setTime(date.getTime() + 24 * 60 * 60 * 1000);
      const dayEnd = formatDateTime(date);
      return [dayStart, dayEnd];   //2023-06-12 2023-06-13
    }

    // ==================== 本周时间段 ====================
    function getCurrentWeek() {
      const date = new Date();
      let nowTime = date.getTime();
      // // 表示当前是周几
      let week = date.getDay() > 0 ? date.getDay() : 7;
      let oneDayTime = 24 * 60 * 60 * 1000; // 一天的总ms
      // 本周一时间戳
      const monday = nowTime - (week - 1) * oneDayTime;
      let weekStart = formatDateTime(new Date(monday));
      // 本周日时间戳
      let sunday = nowTime + (7 - week) * oneDayTime;
      let weekEnd = formatDateTime(new Date(sunday));
      return [weekStart, weekEnd];  
    }

    // ====================== 本月时间段 =====================
      function getCurrentMonth() {
        const date = new Date();
        // 获取本月第一天的日期时间
        let monthStart = formatDateTime(new Date(date.getFullYear(), date.getMonth(), 1)); 
        // 获取本月最后一天的日期时间
        let monthEnd = formatDateTime(new Date(date.getFullYear(), date.getMonth() + 1, 0));
        // let monthEnd = formatDateTime(new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59));
        return [monthStart, monthEnd];  //2023-06-01 2023-06-30
      }

    // ====================== 本季时间段 ======================
    function getCurrentSeason() {
      // 获取当前季度:
      const date = new Date();
      let year = date.getFullYear();
      let currMonth = date.getMonth() + 1;
      let currQuarter = Math.floor(
        currMonth % 3 == 0 ? currMonth / 3 : currMonth / 3 + 1
      );
      switch (currQuarter) {
        case 1:
          seasonStart = formatDateTime(new Date(year, 0, 1));
          seasonEnd = formatDateTime(new Date(year, 3, 0));
          break;
        case 2:
          seasonStart = formatDateTime(new Date(year, 3, 1));
          seasonEnd = formatDateTime(new Date(year, 6, 0));
          break;
        case 3:
          seasonStart = formatDateTime(new Date(year, 6, 1));
          seasonEnd = formatDateTime(new Date(year, 9, 0));
          break;
        case 4:
          seasonStart = formatDateTime(new Date(year, 9, 1));
          seasonEnd = formatDateTime(new Date(year, 12, 0));
          break;
      }
      return [seasonStart, seasonEnd];
    }

    // ===================== 最近三个月 ===================
    function getLastThreeMonth() {
      const date = new Date();
      //三个月前,时间戳
      date.setMonth(date.getMonth() - 3);
      const lastThreeMonthStart = formatDateTime(date);
      let lastThreeMonthEnd = formatDateTime(new Date());
      return [lastThreeMonthStart, lastThreeMonthEnd];
    }

    // ======================= 本年时间段 =======================
    function getCurrentYear() {
        const date = new Date();
        let year = date.getFullYear();
        let yearStart = formatDateTime(new Date(year, 0, 1));
        let yearEnd = formatDateTime(new Date(year, 11, 31));
        return [yearStart, yearEnd];   //2023-01-01 2023-12-31
    }

    // ====================== 打印 ==========================
    const day = getCurrentDay();
    const week = getCurrentWeek();
    const month = getCurrentMonth();
    const season = getCurrentSeason();
    const threeMonth = getLastThreeMonth();
    const year = getCurrentYear();
    console.log("今天至明天", day);
    console.log("本周时间段", week);
    console.log("本月时间段", month);
    console.log("本季时间段", season);
    console.log("最近三个月", threeMonth);
    console.log("本年时间段", year);

猜你喜欢

转载自blog.csdn.net/XiaoSen125_/article/details/130863098
今日推荐