谷歌电子表格的时间格式为12h,无法在谷歌应用程序脚本中进行转换 [英] google spreadsheet time format in 12h, unable to convert in google apps script

查看:77
本文介绍了谷歌电子表格的时间格式为12h,无法在谷歌应用程序脚本中进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组从数据库中提取的日期时间数据.问题在于数据分为两列,一列用于日期,一列用于时间.

I have a set of date time data that is drawn from a database. the problem with it is that the data is separated into two columns, one for date and one for time.

我正在尝试编写一个可以将两者结合使用的google apps脚本,因此我可以进行一些有意义的分析,但无济于事.

I am trying to write a google apps script that can combine the two of them so I can do some meaningful analysis but to no avail.

我尝试了常见的getDate(),getYear(),getMonth()或将它们串联在一起,但这是行不通的.

I tried the common getDate(), getYear(), getMonth() or concatetating them but it doesn't work.

这是电子表格的链接. https://docs.google.com/spreadsheets/d/1VjhT7U8N9X3Q9 #gid = 0

Here is the link to the spreadsheet. https://docs.google.com/spreadsheets/d/1VjhT7Hu8rl1wZgFxItf3unU8jN9Gw3905dNgQ9qICyI/edit#gid=0

如果有人知道如何使这些数据更易于管理,请告诉我吗?

If any one knows how to make this data more manageable please do let me know?

谢谢!

推荐答案

这可能是个老问题,但也许我的解决方法对于某些人还是有用的. 我也有类似的经历.试图在一个日期上组成一个startTime和endTime以便填充表单中的选项.

This might be an old question, but maybe my workaround might be usefull for somebody nevertheless. I had similar; trying to compose a startTime and an endTime on one single date in order to populate options in a form.

从电子表格中的活动选择中获取数据:

Getting data from an active selection in a spreadsheet:

var firstRowNumber = sheet.getActiveRange().getRow();
var numRows = sheet.getActiveRange().getNumRows();    
var sourceData= sheet.getRange(firstRowNumber, 1, numRows, sheet.getLastColumn()).getValues();

然后,迭代此数据集中的两个时间值,并替换以下值:

Then, iterating through the (two) time-values in this dataset and replace the value like:

1899年12月30日星期六10:00:00 GMT + 0100(CET)

Sat Dec 30 1899 10:00:00 GMT+0100 (CET)

转换为所需的时间表示形式,例如:

into the desired representation of time like:

0:00

for(var i = 0; i < sourceData.length; i++){
 for(var j = 8; j < 10; j++){
     sourceData[i][j] = Utilities.formatDate(new Date(sourceData[i][j]), "GMT-8", "HH:mm");
     }
}

为了找出GMT的确切偏差,我对GMT +使用了一些反复试验的值.这可能会在不同的时区或夏令时的更改中引起问题.

In order to find out the exact deviation of GMT, I used some trial-and-error values to GMT+. This might cause a problem in different timezones, or with changes of daylight saving time.

在三个活动行上,这会产生可用的开始和结束日期/时间值:

On three active rows, this results in usable start- and ending date/time-values:

2016年12月30日星期五09:00:00 GMT + 0100(CET) 00:00 2016年12月30日星期五12:00:00 GMT + 0100(CET) 01:00

Fri Dec 30 2016 09:00:00 GMT+0100 (CET) 00:00 Fri Dec 30 2016 09:00:00 GMT+0100 (CET) 01:00

2016年12月31日星期六09:00:00 GMT + 0100(CET) 01:30 星期六2016年12月31日星期六09:00:00 GMT + 0100(CET) 02:15

Sat Dec 31 2016 09:00:00 GMT+0100 (CET) 01:30 Sat Dec 31 2016 09:00:00 GMT+0100 (CET) 02:15

2016年12月29日星期四09:00:00 GMT + 0100(CET) 01:30 2016年12月29日星期四09:00:00 GMT + 0100(CET) 02:15

Thu Dec 29 2016 09:00:00 GMT+0100 (CET) 01:30 Thu Dec 29 2016 09:00:00 GMT+0100 (CET) 02:15

我可以使用它填充表单中的选项.

with which I can populate the options in my form.

这篇关于谷歌电子表格的时间格式为12h,无法在谷歌应用程序脚本中进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆