wangyuan0108 / fe-qa

知识和笔记,整理分享,以便提升和巩固
https://github.com/wangyuan0108/blog/issues
13 stars 0 forks source link

js获取当前日期时间的前后时间 #97

Open wangyuan0108 opened 4 years ago

wangyuan0108 commented 4 years ago
this.dateArray = [];
  let myDate = new Date(); //获取今天日期
  myDate.setDate(myDate.getDate() - 7);
  let dateTemp;  // 临时日期数据
  let flag = 1; 
  for (let i = 0; i < 7; i++) {
    dateTemp = myDate.getFullYear() + '-' +(myDate.getMonth()+1)+"-"+myDate.getDate();
    this.dateArray.push({
      date:dateTemp
    });
    myDate.setDate(myDate.getDate() + flag);
  }
wangyuan0108 commented 4 years ago
const dateList = [...Array(7).keys()].map(days => {
    let t = new Date(Date.now() - 86400000 * days);
    let str = `${t.getFullYear()}年${t.getMonth() + 1}月${t.getDate()}日`;
    return str;
});
console.log(dateList);
wangyuan0108 commented 4 years ago
function fun_date(aa){
    var date1 = new Date(),
    time1=date1.getFullYear()+"-"+(date1.getMonth()+1)+"-"+date1.getDate();//time1表示当前时间
    var date2 = new Date(date1);
    date2.setDate(date1.getDate()+aa);
    var time2 = date2.getFullYear()+"-"+(date2.getMonth()+1)+"-"+date2.getDate();
}
fun_date(7);//7天后的日期
fun_date(-7);//7天前的日期