Trying to subtract 5 days from a defined date - Google App Script

喜欢而已 提交于 2019-12-06 12:32:46

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!