微信小程序中怎样根据当前时间拿到上个月的月初和月末日期?
没有思路,请问各位怎样实现
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本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 2无用