简体   繁体   English

添加日期至今-Google脚本

[英]Adding Days to Date - Google Scripts

I'm fairly new to this, so apologies if the answer is glaringly obvious. 我对此还很陌生,因此如果答案很明显,我深表歉意。

I am trying to add and subtract days from today's date. 我正在尝试从今天的日期开始增加和减少几天。 The idea is eventually to calculate the date of next Monday and last Monday. 最终的想法是计算下周一和上周一的日期。

I've looked here which solved my first problem, but the next is perplexing me: subtracting days from today's date works without issue. 我看过这里 ,这解决了我的第一个问题,但接下来的问题使我感到困惑:从今天的日期中减去天数就没有问题。 Adding gives an invalid date. 添加给出无效的日期。

The code is: 代码是:

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);

The Log Output is: 日志输出为:

[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)

This is irrespective of the value I add to the date. 这与我添加到日期的值无关。 I can't see why subtracting works fine, but adding causes an invalid date. 我看不出为什么减法效果很好,但是加法会导致无效的日期。 Changing the numbers added has no effect, changing it to subtraction works. 更改添加的数字无效,将其更改为减法即可。

That's because JavaScript handles addition and subtraction different depending on the types of the operands. 这是因为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

In your example, you should try converting makeDate to milliseconds before performing addition, either by doing +makeDate or makeDate.getTime() . 在您的示例中,您应该尝试通过执行+makeDatemakeDate.getTime()makeDate转换为毫秒,然后再执行加法操作。

For example, instead of: 例如,代替:

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

Try: 尝试:

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

That is extremely convoluted code for getting next Monday. 下周一要获取的代码非常复杂。 Perhaps it works, but consider a far simpler algorithm. 也许可以,但是考虑一个简单得多的算法。 Hopefully the comments are sufficient: 希望这些评论足够:

 /* 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()); 

Here's a similar function for getting the previous Monday: 这是获取上一个星期一的类似功能:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM