立即获取谷歌appScript日期

use*_*814 22 google-apps-script

如何在Google Appscript上获取今日约会?

我需要在一个单元格中编写一个输入日期的代码

function changeDate(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(GA_CONFIG);
  var date = //Today´s date!?!?!!?
  var endDate = date;

  sheet.getRange(5, 2).setValue(endDate);

 }
Run Code Online (Sandbox Code Playgroud)

小智 38

Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy")
Run Code Online (Sandbox Code Playgroud)

您可以通过交换值来更改格式.

  • dd =天(31)
  • MM =月(12) - 区分大小写
  • yyyy =年(2017年)
function changeDate() {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(GA_CONFIG);
    // You could use now Date(); on its own but it will not look nice.
    var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy")
    var endDate = date
}
Run Code Online (Sandbox Code Playgroud)

  • 这不是一个好主意,`Utilities.formatDate()`返回的值是字符串,而不是日期对象...您将无法对其进行任何操作...以更改单元格中的显示格式同时保留日期对象的属性,请使用[setNumberFormat(numberFormat)](https://developers.google.com/apps-script/reference/spreadsheet/range#setNumberFormat(String))进行完整说明,并使用[this doc](https:以及//developers.google.com/sheets/api/guides/formats) (3认同)

Vig*_*h R 7

Date对象用于处理日期和时间.

使用创建日期对象 new Date()

var now = new Date();
Run Code Online (Sandbox Code Playgroud)

now - 当前日期和时间对象.

function changeDate() {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(GA_CONFIG);
    var date = new Date();
    sheet.getRange(5, 2).setValue(date); 
}
Run Code Online (Sandbox Code Playgroud)


Ser*_*sas 6

Google Apps Script 是 JavaScript,日期对象是用它启动的new Date()并且所有 JavaScript 方法都适用,请参阅此处的文档


小智 6

以下可用于获取日期:

\n\n
function date_date() {\nvar date = new Date();\nvar year = date.getYear();\nvar month = date.getMonth() + 1;  if(month.toString().length==1){var month = \n'0'+month;}\nvar day = date.getDate(); if(day.toString().length==1){var day = '0'+day;}\nvar hour = date.getHours(); if(hour.toString().length==1){var hour = '0'+hour;}\nvar minu = date.getMinutes(); if(minu.toString().length==1){var minu = '0'+minu;}\nvar seco = date.getSeconds(); if(seco.toString().length==1){var seco = '0'+seco;}\nvar date = year+'\xc2\xb7'+month+'\xc2\xb7'+day+'\xc2\xb7'+hour+'\xc2\xb7'+minu+'\xc2\xb7'+seco;\nLogger.log(date);\n}\n
Run Code Online (Sandbox Code Playgroud)\n