从日期范围获取数周

时间:2018-05-02 08:07:43

标签: javascript

我需要根据给定的日期范围提取周数。

以下是我的代码,它将返回NO周数

WeekByDates() {
    let stDate = moment((dates[0].value)).format('YYYY-MM-DD'); //2018-06-01
    let eDate = moment((dates[1].value)).format('YYYY-MM-DD');//2018-06-14

    let startDate = moment(stDate);
    let endDate = moment(eDate);
    let weekData = [];

    while (startDate.isSameOrBefore(endDate)) {
      if (weekData.length > 0) {
        // Update end date
        let lastObj = weekData[weekData.length - 1];
        lastObj['endDate'] = moment(startDate).format('YYYY-MM-DD');
        lastObj['label'] = `${moment(lastObj.startDate).format('MM/DD')} - ${moment(lastObj['endDate']).format('MM/DD')}`
        startDate.add(1, 'days');
      }
      weekData.push({ startDate: moment(startDate).format('YYYY-MM-DD') });
      startDate.add(6, 'days');
    }
      if (startDate.isAfter(endDate)) {
      // Update last object
      let lastObj = weekData[weekData.length - 1];
      lastObj['endDate'] = moment(endDate).format('YYYY-MM-DD');
      lastObj['label'] = `${moment(lastObj.startDate).format('MM/DD')} - ${moment(lastObj['endDate']).format('MM/DD')}`
    }                                                     
    console.log(weekData)
    return weekData;
  }

预期周应

06/01 - 06/07
06/08 - 06/14

但实际上它会返回

06/01 - 06/07
06/08 - 06/14
06/15 - 06/14

不确定我在这里做错了什么。请帮忙

2 个答案:

答案 0 :(得分:0)

在将值推送到weekdata以及

之前,您需要添加以下检查
while (startDate.isSameOrBefore(endDate)) {
  if (weekData.length > 0) {
    // Update end date
    let lastObj = weekData[weekData.length - 1];
    lastObj['endDate'] = moment(startDate).format('YYYY-MM-DD');
    lastObj['label'] = `${moment(lastObj.startDate).format('MM/DD')} - ${moment(lastObj['endDate']).format('MM/DD')}`
    startDate.add(1, 'days');
  }
  if (startDate.isAfter(endDate)) { //check before pushing
    weekData.push({ startDate: moment(startDate).format('YYYY-MM-DD') });
    startDate.add(6, 'days');
  }
}

答案 1 :(得分:0)

您还需要检查推送到阵列的时间Glide.with(context) .load(path) .into(view);



if (moment(startDate).isSameOrBefore(endDate))

function WeekByDates() {
    let stDate = moment('2018-06-01').format('YYYY-MM-DD');
    let eDate = moment('2018-06-14').format('YYYY-MM-DD');

    let startDate = moment(stDate);
    let endDate = moment(eDate);
    let weekData = [];

    while (startDate.isSameOrBefore(endDate)) {
      if (weekData.length > 0) {
        // Update end date
        let lastObj = weekData[weekData.length - 1];
        lastObj['endDate'] = moment(startDate).format('YYYY-MM-DD');
        lastObj['label'] = `${moment(lastObj.startDate).format('MM/DD')} - ${moment(lastObj['endDate']).format('MM/DD')}`
        startDate.add(1, 'days');
      }
      if (moment(startDate).isSameOrBefore(endDate))
      weekData.push({ startDate: moment(startDate).format('YYYY-MM-DD') });
      startDate.add(6, 'days');
    }
      if (startDate.isAfter(endDate)) {
      // Update last object
      let lastObj = weekData[weekData.length - 1];
      lastObj['endDate'] = moment(endDate).format('YYYY-MM-DD');
      lastObj['label'] = `${moment(lastObj.startDate).format('MM/DD')} - ${moment(lastObj.endDate).format('MM/DD')}`
    }                                                     
    console.log(weekData)
    return weekData;
  }
  
  WeekByDates();