JavaScript获得本周第一天的问题

时间:2014-05-07 04:39:09

标签: javascript

我有一个JavaScript问题,可以获得一周的第一天。
它适用于大多数情况,但是当到达本月的第一天时,它会给出错误的结果。

我在这里遗漏了什么吗?

请考虑以下两种情况:

1)2014年2月2日的案例:

<script>
var d = new Date(Date.UTC(2014, 4, 2, 0, 0, 0));
    var day = d.getUTCDay();
    var indate = d.getUTCDate();
    var diff = indate - day + (day == 0 ? -7:0); // adjust when day is sunday
    var sunday = new Date(d.setDate(diff));
    document.write('\nday:' + day);
    document.write('\nindate:' + indate);
    document.write('\nsunday:' + sunday);
    document.write('\diff:' + diff);

</script>
  

结果:日:5 indate:2 sunday:Sun Apr 27 2014 17:00:00 GMT-0700   (太平洋夏令时)差异:-3

2)2014年5月1日的案例:

<script>
var d = new Date(Date.UTC(2014, 4, 1, 0, 0, 0));
    var day = d.getUTCDay();
    var indate = d.getUTCDate();
    var diff = indate - day + (day == 0 ? -7:0); // adjust when day is sunday
    var sunday = new Date(d.setDate(diff));
    document.write('\nday:' + day);
    document.write('\nindate:' + indate);
    document.write('\nsunday:' + sunday);
    document.write('\diff:' + diff);

</script>
  

结果:日:4 indate:1 sunday:2014年3月28日星期五17:00:00 GMT-0700(太平洋   日光时间)差异:-3

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

导致这种情况有两件事:

  1. 使用UTC,您使用的日期值是前一天,晚上。因此,在5月1日,它正在使用4月的最后一天。
  2. setDate方法有一个特殊情况 - 当参数为负数时,它表示前一个月最后一天之前的天数。因为第1点,这意味着在5月1日(被视为4月的最后一次),它会一直回到3月31日,然后在3天内重新开始,给你3月28日。
  3. 您需要首先考虑时区/ UTC问题,然后考虑月边界问题。

    1. 要解决第一个问题,请使用setDateUTC而不是setDate
    2. 第二个问题比较棘手。我能找到的唯一解决方案是减去毫秒而不是几天。

      new Date(d + diff * 24 * 3600 * 1000) // convert days to hours, then seconds, then milliseconds
      

答案 1 :(得分:0)

你可以看看

    var d = new Date(Date.UTC(2014, 4, 2, 0, 0, 0));
    var day = d.getUTCDay();
    var indate = d.getUTCDate();
    var diff = indate - day + (day == 0 ? -6:0); // adjust when day is sunday
    var sunday = new Date(d.setDate(diff));
    document.write('\nday:' + day);
    document.write('\nindate:' + indate);
    document.write('\nsunday:' + sunday);
    document.write('\diff:' + diff);

    document.write('<br/>');

    var d = new Date(Date.UTC(2014, 4, 1, 0, 0, 0));
    var day = d.getUTCDay();
    var indate = d.getUTCDate();
    var diff = indate - day + (day == 0 ? -6:0); // adjust when day is sunday
    var sunday = new Date(d.setDate(diff));
    document.write('\nday:' + day);
    document.write('\nindate:' + indate);
    document.write('\nsunday:' + sunday);
    document.write('\diff:' + diff);

输出

day:5 indate:2 sunday:Sun Apr 27 2014 05:30:00 GMT+0530 (India Standard Time)diff:-3
day:4 indate:1 sunday:Sun Apr 27 2014 05:30:00 GMT+0530 (India Standard Time)diff:-3

答案 2 :(得分:0)

尝试以下方法:

var d = new Date(Date.UTC(2014, 4, 1, 0, 0, 0)); var day = d.getUTCDay(); var indate = d.getUTCDate(); var diff = indate - day; if (diff < 0) diff = d.getDate() + diff; var sunday = new Date(d.setDate(diff)); document.write('\nday:' + day); document.write('\nindate:' + indate); document.write('\nsunday:' + sunday); document.write('\diff:' + diff); document.write('\dateNumber:' + dateNumber);