js获取当天日期,昨天日期,七天前日期,30天前日期,近三个月日期

当天时间:

const today = new Date().toISOString().slice(0, 19).replace("T", " ");
this.start = today.split(" ")[0];

昨天时间:

function getDateStr(AddDayCount) {
          var dd = new Date();
          dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
          var year = dd.getFullYear();
          var mon = dd.getMonth() + 1; //获取当前月份的日期
          var day = dd.getDate();
          return year + "-" + "0" + mon + "-" + day;
        }
this.start = getDateStr(-1);

七天前时间:

function toDay() {
          //获取当前日期
          let myDate = new Date();
          let nowY = myDate.getFullYear();
          let nowM = myDate.getMonth() + 1;
          let nowD = myDate.getDate();
          let endDate =
            nowY +
            "-" +
            (nowM < 10 ? "0" + nowM : nowM) +
            "-" +
            (nowD < 10 ? "0" + nowD : nowD); //当前日期
          // console.log(endDate);
          return endDate;
          // this.star = endDate;
 }
 function toEnd() {
          let myDate = new Date();
          let lw = new Date(myDate - 1000 * 60 * 60 * 24 * 7); //最后一个数字7可改,7天的意思
          let lastY = lw.getFullYear();
          let lastM = lw.getMonth() + 1;
          let lastD = lw.getDate();
          let startData =
            lastY +
            "-" +
            (lastM < 10 ? "0" + lastM : lastM) +
            "-" +
            (lastD < 10 ? "0" + lastD : lastD); //三十天之前日期
          // console.log(startData);
          return startData;
          // this.end = startData;
        }
 let d1 = toDay()
 let d2 = toEnd()

30天起始日期:

function toDay() {
          //获取当前日期
          let myDate = new Date();
          let nowY = myDate.getFullYear();
          let nowM = myDate.getMonth() + 1;
          let nowD = myDate.getDate();
          let endDate =
            nowY +
            "-" +
            (nowM < 10 ? "0" + nowM : nowM) +
            "-" +
            (nowD < 10 ? "0" + nowD : nowD); //当前日期
          // console.log(endDate);
          return endDate;
          // this.star = endDate;
 }
 function toEnd() {
          let myDate = new Date();
          let lw = new Date(myDate - 1000 * 60 * 60 * 24 * 30); //最后一个数字30可改,30天的意思
          let lastY = lw.getFullYear();
          let lastM = lw.getMonth() + 1;
          let lastD = lw.getDate();
          let startData =
            lastY +
            "-" +
            (lastM < 10 ? "0" + lastM : lastM) +
            "-" +
            (lastD < 10 ? "0" + lastD : lastD); //三十天之前日期
          // console.log(startData);
          return startData;
          // this.end = startData;
        }
 let d1 = toDay()
 let d2 = toEnd()

近三个月起始日期:

const today = new Date().toISOString().slice(0, 19).replace("T", " "); //当天日期
// 获取三个月前的日期时间
 const threeMonthsAgo = new Date();
 threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
 const datetimeThreeMonthsAgo = threeMonthsAgo.toISOString().slice(0, 19) .replace("T", " ");
  // 输出结果
console.log("今天的日期时间:", today.split(" ")[0]);
 console.log( "三个月前的日期时间:", datetimeThreeMonthsAgo.split(" ")[0] );
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值