vue 中 使用 js 获取本周、本月、本年的开始日期和结束日期

vue 中 使用 js 获取本周、本月、本年的开始日期和结束日期

1.获取上周、本周、下周的开始日期和结束日期
/**获取本周、下周、上周的起始日、结束日 */
// 1.调用方法 getMonday("s",0)、getMonday("e",0)
// 2.type 为"s"代表开始日期,type为"e"代表结束日期
// 3.dates  不传或0代表本周,-1代表上周,1代表下周 
export const getMonday = (type, dates) => {
  var now = new Date();
  var nowTime = now.getTime();
  var day = now.getDay();
  var longTime = 24 * 60 * 60 * 1000;
  var n = longTime * 7 * (dates || 0);
  if (type == "s") {
      var dd = nowTime - (day - 1) * longTime + n;
  };
  if (type == "e") {
      var dd = nowTime + (7 - day) * longTime + n;
  };
  dd = new Date(dd);
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1;
  var d = dd.getDate();
  m = m < 10 ? "0" + m: m;
  d = d < 10 ? "0" + d: d;
  var day = y + "-" + m + "-" + d;
  return day;
}
2.获取上月、本月、下月的开始日期和结束日期
/**获取本月、上月、下月的起始日、结束日 (不传或0代表本月,-1代表上月,1代表下月)*/
// 1.调用方法 getMonth("s",0)、getMonth("e",0)
// 2.type 为"s"代表开始日期,type为"e"代表结束日期
// 3.dates  不传或0代表本月,-1代表上月,1代表下月
export const getMonth = (type, months) => {
  var d = new Date();
  var year = d.getFullYear();
  var month = d.getMonth() + 1;
  if (Math.abs(months) > 12) {
      months = months % 12;
  };
  if (months != 0) {
      if (month + months > 12) {
          year++;
          month = (month + months) % 12;
      } else if (month + months < 1) {
          year--;
          month = 12 + month + months;
      } else {
          month = month + months;
      };
  };
  month = month < 10 ? "0" + month: month;
  var date = d.getDate();
  var firstday = year + "-" + month + "-" + "01";
  var lastday = "";
  if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" || month == "12") {
      lastday = year + "-" + month + "-" + 31;
  } else if (month == "02") {
      if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
          lastday = year + "-" + month + "-" + 29;
      } else {
          lastday = year + "-" + month + "-" + 28;
      };
  } else {
      lastday = year + "-" + month + "-" + 30;
  };
  var day = "";
  if (type == "s") {
      day = firstday;
  } else {
      day = lastday;
  };
  return day;
}
3.获取去年、今年、明年的开始日期和结束日期
/**获取去年、今年、明年开始日期、结束日期 (不传或0代表今年,-1代表去年,1代表明年)*/
// 1.调用方法 getYear("s",0)、getYear("e",0)
// 2.type 为"s"代表开始日期,type为"e"代表结束日期
// 3.dates  不传或0代表本年,-1代表上年,1代表下年
export const getYear = (type, dates) => {
  var dd = new Date();
  var n = dates || 0;
  var year = dd.getFullYear() + Number(n);
  if (type == "s") {
      var day = year + "-01-01";
  };
  if (type == "e") {
      var day = year + "-12-31";
  };
  if (!type) {
      var day = year + "-01-01/" + year + "-12-31";
  };
  return day;
};
4.获取当前日期
/**获取当前日期 */
// 直接调用 getCurrentDate()
export const getCurrentDate =() =>{
  let nowDate = new Date();
  let date = {
      year: nowDate.getFullYear(),
      mosysnth: nowDate.getMonth() + 1,
      date: nowDate.getDate(),
  }
  
  let systemDate = date.year + '-' + appendZero(date.mosysnth) + '-' + appendZero(date.date);
  return systemDate;
}

// 如果月份和天数小于10自动加0
export const appendZero = (obj) => {
  if(obj < 10){
    return "0" + obj
  } else{
    return obj
  }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值