简体   繁体   English

如何在Google Apps脚本中处理日期?

[英]How to handle dates in Google Apps Script?

How to handle dates in GAS? 如何在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. 我的表单使用的是JQuery datepicker,我需要将选定的日期插入一个单元格中,并将该日期再加上一个月。

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 虽然选择的日期是2014年12月12日

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. 我认为您需要正确构造日期,因为您引用的是datepicker对象,它不是日期,所以请从中获取日期。 Try: new Date(form.datepicker.getDate().getTime()) 试试: new Date(form.datepicker.getDate().getTime())
  • You can't 'plus one' to a date object. 您不能对日期对象“加一”。 Call setMonth instead (it's range-proof). 而是调用setMonth(它是范围内的)。 Try this: date.setMonth(date.getMonth()+1) 试试这个: date.setMonth(date.getMonth()+1)

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

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