添加天数 - Google脚本

时间:2016-05-22 14:27:18

标签: javascript date google-apps-script google-sheets

我对此很新,如果答案显而易见,请道歉。

我正在尝试添加和减去今天的日期。这个想法最终会计算下周一和上周一的日期。

我看了here这解决了我的第一个问题,但接下来让我感到困惑:从今天的日期减去几天没有问题。添加提供无效日期。

代码是:

function findDate() {
   var d = new Date();
   var n = d.getDay();
   var makeDate = new Date(d.setDate(d.getDate()));
   Logger.log(makeDate)
   var weekDaysTo = new Array(7); //array of days to following Monday
   weekDaysTo[0]=  1
      weekDaysTo[1] = 7
      weekDaysTo[2] = 6
      weekDaysTo[3] = 5
      weekDaysTo[4] = 4
      weekDaysTo[5] = 3
      weekDaysTo[6] = 2

  Logger.log('weekDaysTo gives '+weekDaysTo[n])   

  var weekDaysFrom = new Array(7); //array of days to previous Monday
      weekDaysFrom[0]=  6
      weekDaysFrom[1] = 0
      weekDaysFrom[2] = 1
      weekDaysFrom[3] = 2
      weekDaysFrom[4] = 3
      weekDaysFrom[5] = 4
      weekDaysFrom[6] = 5

  var prevMon = new Date(makeDate+weekDaysTo[n]*3600000*24); //Converts ms     into days and adds

  Logger.log('Next Monday is '+prevMon);

  var followingMon = new Date(makeDate-weekDaysFrom[n]*3600000*24); //Converts ms into days
  Logger.log('Last Monday was ' +followingMon);

日志输出是:

[16-05-22 21:17:58:419 ICT] Sun May 22 21:17:58 GMT+07:00 2016
[16-05-22 21:17:58:419 ICT] weekDaysTo gives 1
[16-05-22 21:17:58:420 ICT] Next Monday is Invalid Date 
[16-05-22 21:17:58:420 ICT] Last Monday was Mon May 16 2016 21:17:58 GMT+0700 (ICT)

这与我在日期中添加的值无关。我不明白为什么减法工作正常,但添加导致无效的日期。更改添加的数字无效,将其更改为减法效果。

2 个答案:

答案 0 :(得分:1)

那是因为JavaScript根据操作数的类型处理加法和减法的不同。

> new Date()
Sun May 22 2016 14:45:00 GMT+0000 (UTC)
> new Date()+1
'Sun May 22 2016 14:45:05 GMT+0000 (UTC)1'
> new Date()-1
1463928307077

在您的示例中,您应该在执行添加之前尝试将makeDate转换为毫秒,方法是执行+makeDatemakeDate.getTime()

例如,而不是:

var prevMon = new Date(makeDate+weekDaysTo[n]*3600000*24);

尝试:

var prevMon = new Date(+makeDate+weekDaysTo[n]*3600000*24);

答案 1 :(得分:0)

这是下周一获得的非常复杂的代码。也许它有效,但考虑一个更简单的算法。希望这些评论足够:



/* Given a date, return a new date for the following Monday.
** If date is a Monday, returns the following Monday.
**
** @param {Date} date - date to get next Monday of. 
**                    - Default is current date.
** @returns {Date}    - date for next Monday.
*/
function getNextMonday(date) {
  // Copy provided date or use current date
  date = date? new Date(+date) : new Date();

  // Get the day number
  var dayNum = date.getDay();

  // Set date to next Monday
  date.setDate(date.getDate() + (dayNum? 8 - dayNum: 1));
  return date;
}

var x = new Date(2016,4,29)
document.write('Start date: ' + x + '<br>Next Monday: ' + getNextMonday(x));
x = new Date();
document.write('<br><br>Start date: ' + x + '<br>Next Monday: ' + getNextMonday());
&#13;
&#13;
&#13;

这是获得上周一的类似功能:

function getPreviousMonday(date) {
  date = date? new Date(+date) : new Date();
  var dayNum = date.getDay();
  // If Sunday, subtract 6. If Monday, subtract 7. Otherwise subtract dayNum - 1
  date.setDate(date.getDate() - (dayNum? (dayNum - 1 || 7) : 6));
  return date;
}