记录:获取当天本周本月开始及结束时间并格式化年-月-日 时:分:秒

创建time.js

// 获取指定日期的 00:00:00 时间
export function getStartDate(date) {
    date = date || new Date();
    date.setHours(0, 0, 0, 0);
    return date;
}

// 获取指定日期的 23:59:59 时间
export function getEndDate(date) {
    date = date || new Date();
    date.setHours(23, 59, 59, 999);
    return date;
}

// 获取本周的起始日期和结束日期
export function getCurrentWeek() {
    const currentDate = new Date();
    const currentDayOfWeek = currentDate.getDay() - 1; // 0: Sunday, 1: Monday, ...
    const startDate = new Date(currentDate);
    startDate.setDate(currentDate.getDate() - currentDayOfWeek);
    const endDate = new Date(startDate);
    endDate.setDate(startDate.getDate() + 6);
    return [getStartDate(startDate), getEndDate(endDate)];
}

// 获取当月的起始日期和结束日期
export function getCurrentMonth() {
    const currentDate = new Date();
    const year = currentDate.getFullYear();
    const month = currentDate.getMonth();
    const startDate = new Date(year, month, 1);
    const endDate = new Date(year, month + 1, 0);
    return [getStartDate(startDate), getEndDate(endDate)];
}

// 另一个
export function formatDate(date) {
    var year = date.getFullYear();
    var month = (date.getMonth() + 1).toString().padStart(2, '0');
    var day = date.getDate().toString().padStart(2, '0');
    var hour = date.getHours().toString().padStart(2, '0');
    var minute = date.getMinutes().toString().padStart(2, '0');
    var second = date.getSeconds().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
// 当天
export function getToday() {
    var now = new Date();
    var start = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0);
    var end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59);
    return {
        start: formatDate(start),
        end: formatDate(end)
    };
}
// 24小时内昨天和今天 设备-虫情需要
export function getTodayWithin() {
    var now = new Date();
    now.setDate(now.getDate() - 1);
    var start = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0);
    var end = new Date(now.getFullYear(), now.getMonth(), now.getDate()+1, 23, 59, 59);
    return {
        start: formatDate(start),
        end: formatDate(end)
    };
}
// 近一周
export function getWeek() {
    var now = new Date();
    now.setDate(now.getDate() - 7); // 将当前日期往前推7天
    var weekStart = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 0, 0, 0);
    var weekEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 7, 23, 59, 59);
    return {
      start: formatDate(weekStart),
      end: formatDate(weekEnd)
    };
}

export function getMonth() {
    var now = new Date();
  now.setDate(now.getDate() - 30); // 将当前日期往前推30天
  var monthStart = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 0, 0, 0);
  var monthEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 30, 23, 59, 59);
  return {
    start: formatDate(monthStart),
    end: formatDate(monthEnd)
  };
}
export function getHalfYear() {
    const now = new Date();
  now.setDate(now.getDate() - 180); // 将当前日期往前推180天

  const startDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0);
  const endDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 180, 23, 59, 59);

  return {
    start: formatDate(startDate),
    end: formatDate(endDate),
  };
}
// 格式化日对象
export function getNowDate() {
    var date = new Date();
    var sign2 = ":";
    var year = date.getFullYear() // 年
    var month = date.getMonth() + 1; // 月
    var day = date.getDate(); // 日
    var hour = date.getHours(); // 时
    var minutes = date.getMinutes(); // 分
    var seconds = date.getSeconds() //秒
    var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
    var week = weekArr[date.getDay()];
    // 给一位数的数据前面加 “0”
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (day >= 0 && day <= 9) {
        day = "0" + day;
    }
    if (hour >= 0 && hour <= 9) {
        hour = "0" + hour;
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
    }
    if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
    }
    return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
}
//  处理时间展示年月日
export function formatDateWithoutTime(datetime) {
    const date = new Date(datetime);
    const year = date.getFullYear();
    const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
    const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    return `${year}-${month}-${day}`;
}

需要使用的地方引入

import { getToday, getWeek, getNowDate,getCurrentWeek ,getMonth,getCurrentMonth} from "@/utils/time";

使用:

submitXh(index){
      let str=''
      let end=''
      if(index==1){//获取当天开始时间结束时间
        const date=getToday()
        console.log(date);
        str = date.start;
        end = date.end;
      }else if(index==2){//获取本周开始、结束时间并格式化年-月-日 时:分:秒
        const date=getCurrentWeek()
        // 将ISO格式的时间字符串转换为Date对象
          const start = new Date(date[0]);
          const ends = new Date(date[1]);
          // 设置时区为UTC+8
          start.setHours(start.getHours() + 8);
          ends.setHours(ends.getHours() + 8);
          // 格式化输出
          const startDateStr = start.toISOString().replace("T", " ").slice(0, -5);
          const endDateStr = ends.toISOString().replace("T", " ").slice(0, -5);
          console.log(date);
          str = start.toISOString().replace("T", " ").slice(0, -5);
          end = ends.toISOString().replace("T", " ").slice(0, -5);
      }else{获取本月开始、结束时间并格式化年-月-日 时:分:秒
        const date=getCurrentMonth()
        console.log(date);
        // 将ISO格式的时间字符串转换为Date对象
        const start = new Date(date[0]);
        const ends = new Date(date[1]);
        // 设置时区为UTC+8
        start.setHours(start.getHours() + 8);
        ends.setHours(ends.getHours() + 8);
        str = start.toISOString().replace("T", " ").slice(0, -5);
        end = ends.toISOString().replace("T", " ").slice(0, -5);
      }
    
      
    },

你可能感兴趣的:(javascript,前端,typescript)