Domanda

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

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top