简体   繁体   English

如何将UNIX时间戳转换为Google Sheets日期值?

[英]How can I turn a UNIX timestamp into a Google Sheets date value?

I'm using Google Apps Script to retrieve some data through an API. 我正在使用Google Apps脚本通过API检索一些数据。 The date that is given to me is a standard ISO 8601 string, and I need to convert that to a date number that will be understood by Google Sheets when I display it (ie as the Google Sheets / Excel standard date value) 给我的日期是标准的ISO 8601字符串,我需要将其转换为显示时Google表格可以理解的日期数字(即Google表格/ Excel标准日期值)

Currently I'm using a workaround which is passing a string in my cells then converting it through the DATEVALUE() function but it is not optimal, since I cant use that function natively in my script (from my research on the topic). 当前,我正在使用一种变通方法,该方法是在单元格中传递字符串,然后通过DATEVALUE()函数将其转换,但这并不是最佳选择,因为我无法在脚本中本地使用该函数(来自本主题的研究)。

I want to create a function directly in Google Apps script that converts the Javascript ol' Date() object into a readable value for my spreadsheet. 我想直接在Google Apps脚本中创建一个函数,该函数将Javascript的Date()对象转换为电子表格的可读值。

I've made a few attempts by converting the seconds since 1970 to the number of days since 1900 but I always have a two-hour gap that I can't explain. 我通过将自1970年以来的秒数转换为自1900年以来的天数进行了一些尝试,但是我总是有两个小时的时间间隔,无法解释。 I suspect it has to do with the fact that I am in UTC+2, but can't really solve it, and adding two hours sounds like a bad fix. 我怀疑这与我使用UTC + 2的事实有关,但并不能真正解决问题,而添加两个小时听起来似乎是一个不好的解决方案。

The baseline of what I've gotten to so far is this : 到目前为止,我所获得的基线是:

function toDateNum(string) {

  //Parsing to Unix Timestamp
  date = new Date(string).getTime() / 1000

  //Here I need convert to GSheets date value, add the number of days between 1900-01-01 and 1970-01-01


  return date ;

}

I'm looking either for an arithmetic solve for this (converting and adding dates) or for a function integrated in the Google Sheets app that would allow me to work with this. 我正在寻找对此的算术求解(转换和添加日期),或者正在Google表格应用程序中集成的函数可以让我使用此函数。 Any suggestions ? 有什么建议么 ?

Thanks a lot ! 非常感谢 !

EDIT (with additional clarification) 编辑(附加说明)

One of my issues is that the toISOString format returned is not supported by google sheets directly. 我的问题之一是Google表格不直接支持返回的toISOString格式。 It will only be read as a string, not as a date value. 它将仅作为字符串读取,而不作为日期值读取。 Currently, I'm working around this by sending the toISOString value in the cells and reformat using DateValue() directly in the table. 目前,我正在通过在单元格中发送toISOString值并直接在表中使用DateValue()重新格式化来解决此问题。 I'm trying to do that natively in the script. 我正在尝试在脚本中本地执行此操作。

To the best of my knowledge, the date value supported by Google Sheets (and Excel) is the number of days since 1900-01-01. 据我所知,Google表格(和Excel)支持的日期值为自1900-01-01以来的天数。 A simple arithmetic solution should be found (computing a unix timestamp from seconds then to days then add the difference between 1900-01-01 to 1970-01-01) but, then again, I get my two-hour gap that messes everything up. 应该找到一个简单的算术解决方案(计算从几秒钟到几天的unix时间戳,然后加上1900-01-01与1970-01-01之间的差),但是再一次,我又得到了两个小时的时间间隔。

In the question linked below, the response by user79865 explains how to manage dates and their formats within sheets. 在下面链接的问题中,user79865的响应说明了如何在工作表中管理日期及其格式。 The Sheets API documentation also specifies all the different formats you can use as well as how to create your own when working with dates and times. Sheets API文档还指定了您可以使用的所有不同格式,以及在处理日期和时间时如何创建自己的格式。 The Utilities.formatDate() will help you change the date format to whatever you need. Utilities.formatDate()将帮助您将日期格式更改为所需的格式。

Question URL: https://webapps.stackexchange.com/questions/88621/basic-date-manipulation-in-google-script?newreg=7c66fdcf156f4ff5a30eb5fa4153b243 问题网址: https//webapps.stackexchange.com/questions/88621/basic-date-manipulation-in-google-script?newreg = 7c66fdcf156f4ff5a30eb5fa4153b243

Sheets Documentation URL: https://developers.google.com/sheets/api/guides/formats 表格文档网址: https//developers.google.com/sheets/api/guides/formats

You can achieve this by using new Date(string*1000) , then passing the result to Utilities.formatDate() . 您可以通过使用new Date(string*1000)并将结果传递到Utilities.formatDate() Multiplying the Unix timestamp by 1000 means your argument is in milliseconds, rather than seconds. 将Unix时间戳乘以1000表示您的参数以毫秒为单位,而不是秒。 This Stackoverflow Question has a lot of good info for formatting dates using js. 这个Stackoverflow问题有很多关于使用js格式化日期的好信息。

function toDateNum(string) {
  //convert unix timestamp to milliseconds rather than seconds
  var d = new Date(string*1000);

  //get timezone of spreadsheet
  var tz = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone();

  //format date to readable format
  var date = Utilities.formatDate(d, tz, 'dd-MM-yyyy hh:mm:ss a');

  return date;
}

This script will use the timezone setting of your spreadsheet, but you can change this to use the time zone of the script or enter one yourself, examples below if you'd like to use these instead: 该脚本将使用电子表格的时区设置,但是您可以将其更改为使用脚本的时区,也可以自己输入一个,如果您想改用以下示例,请参见以下示例:

//use spreadsheet timezone setting
var tz = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone();

//use script timezone setting
var tz = Session.getScriptTimeZone();

//enter your own time zone
var tz = 'GMT+0';

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

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