时间 公用方法当前时间,昨天,,本周,上周,本月,上月,今年至上月

/**
 * 日期范围工具类
 */
 export const startTime = (time) => {
  let currTime=formatDate(time) + " 00:00:00"
  return currTime
}

 export const endTime=(time) => {
  let currTime=formatDate(time) + " 23:59:59"
  return currTime
}
//格式化时间
export const formatDate=(nowTimeDate) =>{
  let year=nowTimeDate.getFullYear()
  let month=nowTimeDate.getMonth()+1
  let date=nowTimeDate.getDate()
  let time=year+'-'+ month.toString().padStart(2,"0") +'-'+ date.toString().padStart(2,"0")
  return time
}

/***
 * 当前时间
 */
 export const getCurrentDate=() => {
  return new Date();
}

/***
 * 今天的开始时间
 */
 export const getBeginToday=()  =>{
  return new Date(new Date(new Date().toLocaleDateString()).getTime());
}

/***
 * 昨天开始时间
 */
 export const getBeginYesterday=()  =>{
  return startTime(getBeginToday() - 24 * 60 * 60 * 1000);
}


/***
 * 昨天结束时间时间
 */
 export const getEndYesterday=() => {
  return endTime(getBeginToday() - 24 * 60 * 60 * 1000);
}
/***
 * 本周的第一天时间
 */
 export const getBeginWeek=()  => {
  let currentDate = getCurrentDate();
  let week = currentDate.getDay();

  //一天的毫秒数
  let millisecond = 1000 * 60 * 60 * 24;
  //减去的天数
  let minusDay = week != 0 ? week - 1 : 6;
  //本周 周一
  let monday = new Date(currentDate.getTime() - (minusDay * millisecond));
  return startTime(monday);
}

/***
 * 本周的最后一天时间
 */
 export const getEndWeek=()  => {
  let currentDate = getCurrentDate();
  let week = currentDate.getDay();
  //一天的毫秒数
  let millisecond = 1000 * 60 * 60 * 24;
  //减去的天数
  let minusDay = week != 0 ? week - 1 : 6;
  //本周 周日
  let monday = new Date(currentDate.getTime() - (minusDay * millisecond));
  let sunday = new Date(monday.getTime() + (6 * millisecond));
  //返回
  return endTime(sunday);
}

/***
 * 上周的开始
 */
 export const getBeginLastWeek=()  =>{
  let currentDate = getCurrentDate();
  let first = currentDate.getDate() - currentDate.getDay() - 6;
  let startDate = new Date(currentDate.setDate(first));
  return startTime(startDate);
}

/***
 * 上周的结束
 */
 export const getEndLastWeek=()  =>{
  let currentDate = getCurrentDate();
  let first = currentDate.getDate() - currentDate.getDay() - 6;
  let last = first + 6;
  let endDate = new Date(currentDate.setDate(last));
  return endTime(endDate);
}

/***
 * 本月的第一天时间
 */
 export const getBeginMonth=()  =>{
  let currentDate = getCurrentDate();
  let currentMonth = currentDate.getMonth();
  //获得当前年份4位年
  let currentYear = currentDate.getFullYear();
  //求出本月第一天
  let firstDay = new Date(currentYear, currentMonth, 1);

  return firstDay;
};

/***
 * 本月的最后一天时间
 */
 export const getEndMonth=() => {
  //获取当前时间
  let currentDate = getCurrentDate();
  let fullYear = currentDate.getFullYear();
  let month = currentDate.getMonth() + 1; // getMonth 方法返回 0-11,代表1-12月
  let endOfMonth = new Date(fullYear, month, 0);
  return endTime(endOfMonth);
};

/***
 * 上月的第一天时间
 */
 export const getBeginLastMonth=()  =>{
  //获取当前时间
  let currentDate = getCurrentDate();
  //获得当前月份0-11
  let currentMonth = currentDate.getMonth();
  //获得当前年份4位年
  let currentYear = currentDate.getFullYear();
  //获得上一个月的第一天
  let priorMonthFirstDay = getPriorMonthFirstDay(currentYear, currentMonth);
  return startTime(priorMonthFirstDay);
};

/***
 * 上月的最后一天时间
 */
 export const getEndLastMonth= ()  =>{
  //获取当前时间
  let currentDate = getCurrentDate()
  //获得当前月份0-11
  let currentMonth = currentDate.getMonth()
  //获得当前年份4位年
  let currentYear = currentDate.getFullYear() // eslint-disable-line no-unused-vars

  //当为12月的时候年份需要加1
  //月份需要更新为0 也就是下一年的第一个月
  if (currentMonth == 11) { //11
    currentYear++ 
    currentMonth = 0 //就为
  } else {
    //否则只是月份增加,以便求的下一月的第一天
    currentMonth++;
  }

  //一天的毫秒数
  let millisecond = 1000 * 60 * 60 * 24;
  //求出上月的最后一天
  let lastDay = new Date(getBeginMonth().getTime() - millisecond);

  return endTime(lastDay);
};
/***
 * 今年第一月份
 */
export const getCurrYearFirst=()=>{
  //获取当前时间
  let currentDate = getCurrentDate()
  let first=new Date(currentDate.getFullYear(), 0, 1)
  return startTime(first)
}
/**
 * 返回上一个月的第一天Date类型
 * @param year 年
 * @param month 月
 **/
 export const getPriorMonthFirstDay=(year, month) => {
  //年份为0代表,是本年的第一月,所以不能减
  if (month == 0) {
    month = 11; //月份为上年的最后月份
    year--; //年份减1
    return new Date(year, month, 1);
  }
  //否则,只减去月份
  month--;
  return new Date(year, month, 1);;
};


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值