获取两个日期内的所有时间

   //获取两日期之间日期列表函数
    getDuringDate(stime,etime){
    console.log("我是开始和结束的时间",stime,etime);

    //初始化日期列表,数组
    this.abscissa = [];
    let i=0;

    //开始日期小于等于结束日期,并循环
    while(stime<=etime){
      this.abscissa[i] = stime;

    //获取开始日期时间戳
      let stime_ts = new Date(stime).getTime();
    //增加一天时间戳后的日期
      let next_date = stime_ts + (24*60*60*1000);

    //拼接月日,这里的月份会返回(0-11),所以要+1
      let next_dates_m = (new Date(next_date).getMonth()+1 < 10)?'0'+(new Date(next_date).getMonth()+1)+'-':(new Date(next_date).getMonth()+1)+'-';
      let next_dates_d = (new Date(next_date).getDate() < 10)?'0'+new Date(next_date).getDate():new Date(next_date).getDate();

      stime = next_dates_m+next_dates_d;
      //增加数组key
      i++;
    }
    console.log(this.abscissa);
  },

注:

       日期显示的是两个时间范围内的,数组中的时间格式和开始时间结束时间的样式有关,根据输入的时间作为循环结束的判断,如果想修改所需时间的格式,则根据循环修改。

猜你喜欢

转载自blog.csdn.net/qq_41550865/article/details/87688720