微信小程序js时间范围计算

demo() { // 初始化默认日期(昨天和今天)
    const today = new Date(); //今天日期
    const yesterday = new Date();//初始化昨日日期(起始日期)
    // -1 代表的是当前日期的头一天 一个月的话就-29就是30天;
  yesterday.setDate(today.getDate() - 1);//时间范围计算 两天范围 当前时间-1天  

  const formatDate = (date) => { //格式化日期
    const year = date.getFullYear(); //年
    const month
= String(date.getMonth() + 1).padStart(2, '0');//月
    const day = String(date.getDate()).padStart(2, '0'); //日
    console.log('默认时间:', `${year}-${month}-${day}`) //输出例如 2025-02-02

    return `${year}-${month}-${day}`;
  };

  
this.setData({
     startDate: formatDate(yesterday),
     endDate: formatDate(today)
   });
},

 

posted @ 2025-05-27 15:55  3939!  阅读(45)  评论(0)    收藏  举报