The example in this article describes the js method of obtaining the seven days before the current date. Share it with everyone for your reference. The details are as follows:
//设置日期,当前日期的前七天 var myDate = new Date(); //获取今天日期 myDate.setDate(myDate.getDate() - 7); var dateArray = []; var dateTemp; var flag = 1; for (var i = 0; i < 7; i++) { dateTemp = (myDate.getMonth()+1)+"-"+myDate.getDate(); dateArray.push(dateTemp); myDate.setDate(myDate.getDate() + flag); }
Supplement:
getDate() returns the day of the month (1 ~ 31) from the Date object.
getMonth() returns the month (0 ~ 11) from a Date object.
getFullYear() Returns the year as a four-digit number from a Date object.
I hope this article will be helpful to everyone’s JavaScript programming design.