简体   繁体   中英

How to handle dates in Google Apps Script?

How to handle dates in GAS?

My form is using JQuery datepicker and I need to insert the chosen date in a cell and that date plus one month on another.

This is what I tried:

  //var datepicker=form.datepicker;
  var datepicker= new Date(form.datepicker);
  //var datepicker=Date.parse(datepicker);
  //var datepicker=datepicker.toString();
  //var datepicker=parseInt(datepicker);
  //var datepicker=datepicker.toDateString();

   sheet.getRange(2, 1).setValue(datepicker);
   sheet.getRange(3, 1).setValue(datepicker+1);

Which is returning:

1970/01/010
Invalid Date1

Although the selected date is 02-12-2014

I see two problems:

  • I think you need to construct the date properly, you're referencing the datepicker object, which isn't a date, so get the date out of it. Try: new Date(form.datepicker.getDate().getTime())
  • You can't 'plus one' to a date object. Call setMonth instead (it's range-proof). Try this: date.setMonth(date.getMonth()+1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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