【问题标题】:How to combine date object and time object into DateTime object and store in google sheet as DateTime object如何将日期对象和时间对象组合成 DateTime 对象并作为 DateTime 对象存储在谷歌表中
【发布时间】:2019-08-16 12:36:00
【问题描述】:

我有一个日期对象和一个时间对象,我想将它们组合成一个日期时间对象,而不是作为一个字符串,而是作为一个日期时间对象,我可以存储它以便以后针对其他日期时间对象进行测试。像这样的所有问题似乎都处理日期时间的字符串格式。

我尝试简单地将两个对象添加在一起,这适用于工作表的两个元素,但不适用于脚本变量。

var lastDayPumpOn = new Date();
lastDayPumpOn = lastPumpDate + lastPumpTime;
Browser.msgBox('Last on time = '+lastDayPumpOn);

这个简单的加法产生的结果似乎是日期为零的日期和日期为零的时间的串联。我不清楚它实际上是一个日期时间对象还是某个字符串。

【问题讨论】:

  • lastPumpTime 似乎是string。你从哪里弄来的?
  • lastPumpDate 是从工作表中读取的,其中值是由“int(lastDate)”从完整日期构造的。lastPumpTime 是从“mod(lastDate,1)”构造的工作表中读取的。我我试图重建完整的 lastDate 以存储在不同的工作表中。
  • 为什么不在表格中引用实际的lastDate,其中包含日期和时间?

标签: date datetime google-apps-script time


【解决方案1】:

JavaScript 中没有“时间”或“日期时间”对象,只有 Date

为什么你得到一个字符串,而不是一个新的日期

如果要将存储在 Date 对象中的 milliseconds 添加在一起,则必须注意两个参数确实是 Date 对象或数字(表示毫秒),并使用 .getTime() 或为变量添加 + 前缀。

// this assumes lastPumpTime is some Date that has had its date component "zeroed out"
var lastDayPumpOn = new Date(lastPumpDate.getTime() + lastPumpTime.getTime()); // construct new Date from milliseconds argument
// or
var lastDayPumpOnAlt = new Date(+lastPumpDate + +lastPumpTime); // implicitly using Date.prototype.valueOf
// lastDayPumpOn.getTime() === lastDayPumpOnAlt.getTime() is true
Browser.msgBox('Last on time = ' + lastDayPumpOn); // implicitly calls lastDayPumpOn.toString()

如果+ 周围的任一变量是字符串,则结果将是字符串。任何对象都会隐式调用其.toString 函数。

清零日期部分

要创建您的“时间”对象(没有日期部分的日期),有多种方法,其中一种是将日期部分设置为 1970 年 1 月 1 日

lastPumpTime.setFullYear(1970);
lastPumpTime.setMonth(0); // Jan === 0, zero indexed
lastPumpTime.setDate(1);

现在lastPumpTime.getTime() 比 24 小时的毫秒值少几毫秒。

【讨论】:

  • 感谢您的建议。我试过了,但收到一条消息“无效日期”。它不是来自“新日期”行,而是来自 msgBox。然后我添加了一个 msgBox 来显示传递给“new Date”的两个值。这产生了“2019 年 7 月 1 日星期一 00:00:00 GMT-0700 (PDT) 1899 年 12 月 30 日星期六 00:00:00 GMT-0800 (PST)”。两者都包含完整的日期和时间,“新日期”似乎无法处理。
  • @AlexanderGrillo lastPumpTime 应该只是一个代表毫秒的数字。您可以减去两个日期来获得这个较小的数字,或者只使用以毫秒为单位的纯数字。
  • 我尝试了类似的尝试来模仿最初在工作表中创建值的方式。我将计算更改为:lastDayPumpOn = new Date(+(Math.floor(lastPumpDate)) +(lastPumpTime - Math.floor(lastPumpTime)))。结果包括正确的日期,但时间是 00:00,而不是正确的时间。事后看来,如果整个日期对象是自纪元以来的总毫秒数,我不知道为什么日期是正确的。
  • 好的。我现在开始明白更多了。我截断日期的尝试什么也没做,因为该对象的时间部分已经是“00:00”。所以我必须有一种方法来取消时间对象的日期部分,以便两者的总和起作用。
  • @AlexanderGrillo - 是的,我在答案中添加了一次这样的截断方法 - 还有其他方法。
【解决方案2】:

我终于让它工作了,所以我想我应该列出工作代码,以防其他人发现它有用。也许我的要求很不寻常,因为我找不到类似的例子。所有其他将日期和时间结合起来的示例都处理字符串格式的日期和时间。
通过这个练习,我学到了很多关于日期操作的知识。 dwmorrin 帮了大忙。最后一个惊喜是需要更正时区。我发现时间修正将结果留在 GMT 区域,实际上 GMT 被夏令时偏移抵消了。我使用 getTimezoneOffset 可能不是解决此问题的最通用方法,但它适用于我的时区。 再次感谢 dwmorrin。

    // The variables lastPumpDate and lastPumpTime are a date and time read from two columns of 
    // dates and times that had been at some earlier time deconstructed from a column of full 
    // date-time objects using the spreadsheet functions int(date) and mod(date,1).  
    // This code segment of a script is to reconstruct the full date-time object to be stored in another 
    // sheet for further processing.  
    Var lastPumpTimeFixed = lastPumpTime;  // Time object must have date component cleared
    lastPumpTimeFixed.setFullYear(1970);       // This done by setting it to base date
    lastPumpTimeFixed.setMonth(0);
    lastPumpTimeFixed.setDate(1);
    var valTimezoneOffset = lastPumpTime.getTimezoneOffset()*60*1000;
    var lastDayPumpOn = new Date(+lastPumpDate.getTime() +lastPumpTimeFixed.getTime() - valTimezoneOffset);

对此解决方案的进一步解释:仅日期对象 lastPumpDate 是通过 .getValues() 函数从现有的 Google 工作表中读取的,该函数还添加了工作表单元格中不存在的时区偏移量。如果/何时将脚本变量与时间对象组合,这会将脚本变量的时间设置为等效的 UTC。此时区偏移基于运行脚本的时区,并包括根据读取日期对夏令时进行的任何调整。仅时间对象 lastPumpTime 同样是使用 .getValues() 函数从预先存在的工作表中读取的,该函数还添加了一个可能在工作表单元格中不存在的时区偏移量,我没有深入了解。此时区偏移对于运行脚本的时区也是正确的,以将时间设置为等效的 UTC,但由于其日期未知,因此无需对夏令时进行任何调整。因此,在“new Date”操作中将仅日期对象添加到仅时间对象时,结果有两个时区偏移量,一个针对夏令时调整,一个不调整。这就是必须减去一个偏移量的原因,它必须是与仅时间对象相关联的偏移量,它不包括夏令时调整。然后,当脚本的后面部分将生成的完整日期时间对象放入新工作表时,剩余的偏移量实际上未应用,使完整日期时间单元格的时间与原始时间单元格的时间相同.我无法确定如果最终工作表与原始工作表位于不同的时区会发生什么,但我想可能会应用不同的调整,使完整日期时间单元格的时间与原始工作表具有相同的 UTC,但经过调整对于不同的时区。

【讨论】:

    猜你喜欢
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    相关资源
    最近更新 更多