C小陈童鞋 2021-09-28 10:24 采纳率: 33.3%
浏览 95
已结题

请问各位,小程序中怎样根据当前时间拿到上个月的月初和月末日期?

微信小程序中怎样根据当前时间拿到上个月的月初和月末日期?
没有思路,请问各位怎样实现

  • 写回答

4条回答 默认 最新

  • tonyzhangcn 2021-09-28 10:59
    关注

    一定要注意跨年的问题哦。下面的这个例子跨年是没有问题的。

    
    let today = new Date();
    temp = new Date(`${today.getFullYear()}-${today.getMonth()+1}`);
    preMonthLastDay = new Date(temp.getTime()-24*60*60*1000);
    preMonthFirstDay = new Date(`${preMonthLastDay.getFullYear()}-${preMonthLastDay.getMonth()+1}`);
    
    console.log(preMonthFirstDay.toLocaleDateString(),preMonthLastDay.toLocaleDateString());
    //输出 2021/8/1 2021/8/31
    

    更多日期相关的操作可以看

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月6日
  • 已采纳回答 9月28日
  • 创建了问题 9月28日