Google Apps 脚本 - 将 n 小时添加到当前时间和日期

Posted

技术标签:

【中文标题】Google Apps 脚本 - 将 n 小时添加到当前时间和日期【英文标题】:Google Apps Script - Adding n Hours to Current Time and Date 【发布时间】:2019-07-05 03:05:02 【问题描述】:

我是 Google Apps 脚本代码的新手,我正在尝试简单地将用户定义的小时数添加到当前时间(例如 12 小时)。然后,该代码将在谷歌文档中插入未来 12 小时的时间和日期。

我使用 ui.prompt 让用户输入他们想要的未来小时数。我拥有的代码没有给我一个错误,但它把当前时间放在未来一些奇怪的天数(不知道它为什么这样做)。这是我拥有的代码,else 部分是我遇到问题的地方...

function UpdateDocument() 

  var document = DocumentApp.getActiveDocument();
  var date = new Date();
  var tomorrow = new Date();
  tomorrow.setDate(tomorrow.getDate()+1);
  var ui = DocumentApp.getUi();

var regExpUpdateDate = "[A-Z]3, [A-Z]3 [A-Z]1, [A-Z]4"; // Regular expression to find the correct line for the Next Update

  // Ask User if the they want to include the Next Update Line  
  var response = ui.alert('Would you like to include the Next Update line?' , ui.ButtonSet.YES_NO);

  // Process the users response.
  if (response == ui.Button.YES) 
    var responseNextUpdate = ui.prompt('Enter the number of hours you want until the next update (i.e. 12, 24, etc.)' 
                                       + ' or leave blank if you only want to include the date and omit the time. Note that'
                                       + ' leaving it blank will default to the day 24 hours from now.'); // Prompts user for number of hours until the next update
    if (responseNextUpdate.getResponseText() == '') 
      document.replaceText(regExpUpdateDate, Utilities.formatDate(tomorrow, 'America/Denver', 'EEEE, MMMM d, yyyy'));
    
    else  // This is the section that I am having issues with...
      var userSelectedHours = new Date();
      userSelectedHours.setDate(userSelectedHours.getHours() + 2);
      document.replaceText(regExpUpdateDate, Utilities.formatDate(userSelectedHours, 'America/Denver', 'h a EEEE, MMMM d, yyyy'));
    
  

【问题讨论】:

【参考方案1】:

我发现使用毫秒来加/减时间更容易。

  var now = new Date(); // Fri Jul 05 09:53:12 GMT+05:30 2019

  var nowInMS = now.getTime(); // 1562300592245

  var add = 12 * 60 * 60 * 1000; // 43200000 = 12 hours in milliseconds

  var twelveHoursLater = nowInMS + add; // 1562343792245

  var futureDate = new Date(twelveHoursLater); // Fri Jul 05 21:53:12 GMT+05:30 2019

  var futureDateFormatted = Utilities.formatDate(futureDate, Session.getScriptTimeZone(), "dd-MMM-yy hh:mm a"); // 05-Jul-19 09:53 PM

【讨论】:

【参考方案2】:

试试这个:

function UpdateDocument() 
  var document = DocumentApp.getActiveDocument();
  var date = new Date();
  var tomorrow = new Date();
  tomorrow.setDate(tomorrow.getDate()+1);
  var ui = DocumentApp.getUi();
  var regExpUpdateDate = "[A-Z]3, [A-Z]3 [A-Z]1, [A-Z]4"; 
  var response = ui.alert('Would you like to include the Next Update line?' , ui.ButtonSet.YES_NO);
  if (response == ui.Button.YES) 
    var responseNextUpdate = ui.prompt('Enter the number of hours you want until the next update (i.e. 12, 24, etc.) or leave blank if you only want to include the date and omit the time. Note that leaving it blank will default to the day 24 hours from now.');
    if (responseNextUpdate.getResponseText() == '') 
      document.replaceText(regExpUpdateDate, Utilities.formatDate(tomorrow, 'America/Denver', 'EEEE, MMMM d, yyyy'));
    
    else  // This is the section that I am having issues with...
      var hours=Number(responseNextUpdate.getResponseText());
      var delayedDate =new Date(new Date().setHours(new Date().getHours() + hours));
      document.replaceText(regExpUpdateDate, Utilities.formatDate(delayedDate, 'America/Denver', 'h a EEEE, MMMM d, yyyy'));
      Logger.log(Utilities.formatDate(delayedDate, 'America/Denver', 'h a EEEE, MMMM d, yyyy'));
    
  

【讨论】:

【参考方案3】:

我遇到了同样的问题,从变量中获取数据并合并到一个新变量中。下面我复制部分代码。它并不顺利,但它正在工作......

  // The commands in the next lines were required to combine date and time
  // event_values[7] corresponds to the date of the event provided by the form to the spreadsheet as dd/MM/yyyy
  var day = new Date(event_values[7].toLocaleString()).getDate().valueOf();
  var month=new Date(event_values[7].toLocaleString()).getMonth().valueOf();
  var year=new Date(event_values[7].toLocaleString()).getFullYear().valueOf();
  // event_values[8] and [9] correspond to the start and end time of the event as HH:mm:ss also provided by the form to the spreadsheet
  var start_time=event_values[8].toString().split(":");
  // since the day was not provided for the start_time and end_time variables, in my case the script assumed as 1899. So I split the string first with ":" and then with "1899 ".
  var start_hour=start_time[0].split("1899 ")[1].valueOf();
  var start_min=start_time[1].valueOf();
  var end_time=event_values[9].toString().split(":")
  var end_hour=end_time[0].split("1899 ")[1].valueOf();
  var end_min=end_time[1].valueOf();

  var event_start_time = new Date(year,month,day,start_hour,start_min,"00");
  var event_end_time=new Date(year,month,day,end_hour,end_min,"00");

【讨论】:

以上是关于Google Apps 脚本 - 将 n 小时添加到当前时间和日期的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Google Apps 脚本将公式添加到 Google 表格?

使用 Google BigQuery / Apps 脚本为插入 Google 表格的数据添加时间戳

用于将 google drive 中的文件名列表与 google sheet 列中的名称列表进行比较的 Apps 脚本,以将新文件从驱动器添加到工作表

使用 Google Apps 脚本自动将标签移动/应用到 Gmail

使用Google Apps脚本Web应用添加到Google表格

Google 表单 - 使用 Google Apps 脚本在项目中添加自定义按钮“更多信息”