小程序
小游戏
企业微信
微信支付
扫描小程序码分享
大佬们,小弟的js太弱了。
比如今天是8月5日,我要算7天之内的日子。=》那就是8月6日,8月7日,。。。8月12日,这样七天。(要把这7天循环出来)随着日子的变化,数据自动变化。这是要用时间戳来计算吧?希望大佬说仔细点。手摸手教学。
效果如下:
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
let now=new Date(), year=now.getFullYear(), month=now.getMonth(), day=now.getDate(), days=[] for(var i=0;i<7;i++){ days.push((new Date(year, month, (day+i)).getMonth()+1)+'月'+new Date(year, month, (day+i)).getDate()+'日') } console.log(days)
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
picker有start 和 end start写今天的日期 end写七天后的日期。
function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds(); return currentdate; } var date = new Date(); //这里的7就是你要加的天数,减也可以。年、月会相应加上去,值得注意的是date.getMonth()得到的月份比实际月份小1,所以实际月份是(date.getMonth()+1) date.setDate(date.getDate() + 7); console.log(date.getFullYear() +"-"+ (date.getMonth()+1) +"-"+ date.getDate());
这个的缺点是格式被固定,其他全是优点
不然就js计算,处理时间用moment.js比较方便
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
let now=new Date(), year=now.getFullYear(), month=now.getMonth(), day=now.getDate(), days=[] for(var i=0;i<7;i++){ days.push((new Date(year, month, (day+i)).getMonth()+1)+'月'+new Date(year, month, (day+i)).getDate()+'日') } console.log(days)
picker有start 和 end start写今天的日期 end写七天后的日期。
function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds(); return currentdate; } var date = new Date(); //这里的7就是你要加的天数,减也可以。年、月会相应加上去,值得注意的是date.getMonth()得到的月份比实际月份小1,所以实际月份是(date.getMonth()+1) date.setDate(date.getDate() + 7); console.log(date.getFullYear() +"-"+ (date.getMonth()+1) +"-"+ date.getDate());
不然就js计算,处理时间用moment.js比较方便