【问题标题】:Get weeks from start and end date in javascript在javascript中从开始日期和结束日期获取周数
【发布时间】:2016-10-03 06:10:26
【问题描述】:

我正在构建一个时间表函数,为此我必须在集合中按周插入数据。

所以我正在创建一个数组,其中包含从 start_date 到 end_date 的周数。

1st Push in an array: start_date = 声明日期(如果声明的日期是星期日,则考虑下周一的日期); end_date = 星期六的日期

第 2 次到第 n 次推入数组: start_date = 星期一的日期; end_date = 星期六的日期或声明的结束日期(如果它在一周内)

var start = new Date("09/30/2016");
var end = new Date("11/2/2016");

var count = 0;
var sDate;
var eDate;
var dateArr = [];

while(start <= end){
    if (start.getDay() == 0){
        count = 0;
    }else {
        if(count == 0){
            sDate = start;
            count = 1
        }else {
            count = 1;
        }

        if(start.getDay() == 6 || start == end){
            count = 1
            eDate = start;
        }else {
            count = 1;
        }

        if(sDate && eDate){
            sDate = new Date(sDate)
            eDate = new Date(eDate)
            dateArr.push({'startDate': sDate, 'endDate': eDate});
            sDate = undefined;
            eDate = undefined;
        }
    }

    var newDate = start.setDate(start.getDate() + 1);
    start = new Date(newDate);
}

但我得到的结果是这样的

[{
    'startDate':Sat Oct 01 2016 00:00:00 GMT+0530 (IST),
    'endDate':Sat Oct 01 2016 00:00:00 GMT+0530 (IST),
},
{
    'startDate':Tue Oct 04 2016 00:00:00 GMT+0530 (IST),
    'endDate':Sat Oct 08 2016 00:00:00 GMT+0530 (IST),
},
{
    'startDate':Tue Oct 11 2016 00:00:00 GMT+0530 (IST),
    'endDate':Sat Oct 15 2016 00:00:00 GMT+0530 (IST),
},
{
    'startDate':Tue Oct 18 2016 00:00:00 GMT+0530 (IST),
    'endDate':Sat Oct 22 2016 00:00:00 GMT+0530 (IST),
},
{
    'startDate':Tue Oct 25 2016 00:00:00 GMT+0530 (IST),
    'endDate':Sat Oct 29 2016 00:00:00 GMT+0530 (IST),
}]

编辑:

预期结果:

[{
    'startDate':Fri Sep 30 2016 00:00:00 GMT+0530 (IST),
    'endDate':Sat Oct 01 2016 00:00:00 GMT+0530 (IST),
},
{
    'startDate':Mon Oct 03 2016 00:00:00 GMT+0530 (IST),
    'endDate':Sat Oct 08 2016 00:00:00 GMT+0530 (IST),
},
{
    'startDate':Mon Oct 10 2016 00:00:00 GMT+0530 (IST),
    'endDate':Sat Oct 15 2016 00:00:00 GMT+0530 (IST),
},
{
    'startDate':Mon Oct 17 2016 00:00:00 GMT+0530 (IST),
    'endDate':Sat Oct 22 2016 00:00:00 GMT+0530 (IST),
},
{
    'startDate':Mon Oct 24 2016 00:00:00 GMT+0530 (IST),
    'endDate':Sat Oct 29 2016 00:00:00 GMT+0530 (IST),
},
{
    'startDate':Mon Oct 31 2016 00:00:00 GMT+0530 (IST)
    'endDate':Wed Nov 02 2016 00:00:00 GMT+0530 (IST),
}]

【问题讨论】:

  • 看看momentsjs
  • @miraculixx 在推入阵列之前,如果 console.log 显示为星期一,则开始日期。我也尝试过使用 moment.js。开始日期仍显示为下一个日期
  • 您需要什么结果(日期之间的周数)?
  • @AlexanderElgin 我需要对象数组中给定日期之间的所有一周的开始日期和结束日期
  • @AlexanderElgin 我在问题中添加了我的预期结果

标签: javascript date


【解决方案1】:

看看这个。我修复了一些逻辑和 obj 引用错误。

var start = new Date(Date.UTC(2016, 09, 30, 0, 0, 0));
var end = new Date(Date.UTC(2016, 11, 02, 0, 0, 0));
var sDate;
var eDate;
var dateArr = [];

while(start <= end){
  if (start.getDay() == 1 || (dateArr.length == 0 && !sDate)){
    sDate = new Date(start.getTime());
  }

  if ((sDate && start.getDay() == 0) || start.getTime() == end.getTime()){
        eDate = new Date(start.getTime());
  }

  if(sDate && eDate){
    dateArr.push({'startDate': sDate, 'endDate': eDate});
    sDate = undefined;
    eDate = undefined;
  }

    start.setDate(start.getDate() + 1);
}

console.log(dateArr);

https://jsfiddle.net/c58zde4b/6/

显示的日期可能因您当地的时区设置而异。

【讨论】:

  • 在这个数组中的第一个对象是星期一,但在我的预期结果中是星期五。所有其他对象的开始日期为星期日。加上对象的数量少了1个
  • 请查看更新后的答案。 - 注意:周日为 0,周一为 1,以此类推。 - 如果日期与小偏移量不匹配,则表明您正在查看正确的时区。
  • 我对您的答案进行了一些更改,这些更改对我有用,请查看它。我会将其标记为答案
  • 我在我的 URL 中看不到您的更改。每次将新更改保存到 jsfiddle 时,都会创建一个新 URL。感谢您选择我的答案:)
  • 答案应该解释 OP 的问题以及您的代码如何修复它。仅发布代码可能对 OP 有所帮助,但对可能遇到相同问题的其他用户(无论是什么)没有帮助。特别是,使用 Date.UTC 有什么意义?