尝试从定义日期减去5天 - Google App脚本 [英] Trying to subtract 5 days from a defined date - Google App Script

查看:158
本文介绍了尝试从定义日期减去5天 - Google App脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个应该发送电子邮件的脚本,并在提交表单时创建两个日历条目。说实话,这是我的第一个脚本,我很高兴邮件发送出去,日历条目也可以正常工作。让我头痛的事情是从定义的日期减去5天(实际上是x天)。



首先,我想我可以简单地做一些类似于

  var liveday = e.values [2]; 
var newday = liveday-5;

好的,这不起作用:-)

我试过更多:

  var newtime = new Date(liveday); 
var yr = newtime.getYear();
var dt = newtime.getDay();
var mt = newtime.getMonth();
var dtnew = dtnew.setDate(mt,dt-5,yr);

但是在这里我收到了 1418256000000 ,而liveday = 2014年12月1日。不知道为什么添加日期,而不是减去。

我在这里很困惑,答案不能那么难。



我只想从2014年12月1日减去5天,收到11/27/2014。



感谢您看一看

解决方案

评论将您发送给相当复杂的一系列代码......还有更多

  function test(){
Logger.log('today ='+ new Date()+'and 5 days ago is'+ subDaysFromDate(new Date(),5));
}

函数subDaysFromDate(date,d){
// d =天数ro substation和date =开始日期
var result = new Date(date .getTime() - d *(24 * 3600 * 1000));
返回结果
}

记录器结果:

  [13-11-18 23:39:50:364 CET] today = Mon Nov 2013 2013 23:39:50 GMT + 0100(CET)and 5天前是Wed Nov 13 2013 23:39:50 GMT + 0100(CET)

如果你想要以dd / mm / yyyy格式获得日期,请使用 Utilities.formatDate(date,timeZone,'dd / MM / yyyy),请参阅doc 此处


I'm trying to write a script which is supposed to send out an email and create two calender entries when submitting a form. To be honest, this is my first script and I am very happy that the email is send out and the calender entries are working as well. The thing which gives me a headache is to subtract 5 days (actually x days) from a defined date.

First I thought I could simply do something like

var liveday = e.values[2];
var newday = liveday-5;

well, this didn't work :-)

I tried more:

var newtime = new Date(liveday);
var yr = newtime.getYear();
var dt = newtime.getDay();
var mt = newtime.getMonth();
var dtnew = dtnew.setDate(mt, dt-5, yr);

But here I received 1418256000000 whereas liveday = 12/01/2014. Not sure why days were added, rather than subtracted.

I am quite confused here and the answer can't be that hard.

I just want to subtract 5 days from 12/01/2014 to receive 11/27/2014.

Thanks for having a look

解决方案

the comment sends you to a rather complicated serie of codes... there is a far more simple way to get that, here is the code :

function test() {
  Logger.log('today= '+new Date()+'  and 5 days ago is '+subDaysFromDate(new Date(),5));
}

function subDaysFromDate(date,d){
  // d = number of day ro substract and date = start date
  var result = new Date(date.getTime()-d*(24*3600*1000));
  return result
}

Logger result :

[13-11-18 23:39:50:364 CET] today= Mon Nov 18 2013 23:39:50 GMT+0100 (CET)  and 5 days ago is Wed Nov 13 2013 23:39:50 GMT+0100 (CET)

if you want to get the date in the form dd/mm/yyyy use Utilities.formatDate(date, timeZone, 'dd/MM/yyyy), see doc here

这篇关于尝试从定义日期减去5天 - Google App脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆