简体   繁体   English

如何在 Google Apps 电子表格脚本中将日期和时间合并为日期时间?

[英]How to merge date and time as a datetime in Google Apps Spreadsheet script?

I've a Date column in 'dd/mm/yyyy' format and Time column in 'HH:MM AM/PM' format in Google App spreadsheet.我在 Google App 电子表格中有一个“dd/mm/yyyy”格式的日期列和“HH:MM AM/PM”格式的时间列。 In app script, I want to join these 2 values as a datetime.在应用程序脚本中,我想将这两个值作为日期时间加入。 How can I do this?我怎样才能做到这一点?

Example- date : 13/12/2010, time : 4:30 PM Then I want to get a combined value like '13/12/2010 4:30 PM' as a datetime object.示例-日期:13/12/2010,时间:4:30 PM 然后我想获得一个组合值,如“13/12/2010 4:30 PM”作为日期时间对象。

I tried to parse date and time separately using getYear, getDay JS functions.我尝试使用 getYear、getDay JS 函数分别解析日期和时间。 And created a new date using并使用创建了一个新日期

new Date(year, month, day, hours, minutes)

But for some reason time '4:30' is being shown as a full weird date like 30 Nov 1899 10:30AM.但是出于某种原因,时间“4:30”被显示为一个完整的奇怪日期,例如 1899 年 11 月 30 日上午 10:30。 I can accurately extract year, day and month but not hours and minutes.我可以准确地提取年、日和月,但不能准确提取小时和分钟。

Ive got this answered in this Google Apps forum thread. 我在这个 Google Apps论坛帖子中得到了回答。

"First, there's no difference between date and time (besides the formatting), they are all Date objects. Now to the problem.. The number which is the value of the date/time in the spreadsheet, is the number of days since the "epoch", so the "time" is actually the decimal part of the number. If you type 0 (zero) in your spreadsheet and then select the datetime format, you'll see that the epoch is "12/30/1899 0:00:00" in the spreadsheet current timezone. ie in different spreadsheets, configured with different timezones, the epoch may change. And if you operate the same number value in two spreadsheet (eg using ImportRange) you'll the different. A behavior that is very odd to me. It was not always like this, but google seemed to think this is better to the user, because if one changes the spreadsheet timezone settings, the dates will not change visually, eg if you type "1/1/10 14:00" in the spreadsheet and then change the timezone to any other, the date/time will not change. It will still “首先,日期和时间之间没有区别(除了格式化),它们都是Date对象。现在问题..电子表格中日期/时间值的数字是自该日期以来的天数“epoch”,所以“时间”实际上是数字的小数部分。如果你在电子表格中键入0(零),然后选择日期时间格式,你会看到纪元是“12/30/1899 0 :00:00“在电子表格当前时区。即在不同的电子表格中,配置了不同的时区,时代可能会发生变化。如果你在两个电子表格中操作相同的数字值(例如使用ImportRange),你就会有所不同。这对我来说很奇怪。它并不总是这样,但谷歌似乎认为这对用户更好,因为如果更改电子表格时区设置,日期将不会在视觉上改变,例如,如果您输入“1/1 / 10 14:00“在电子表格中,然后将时区更改为任何其他,日期/时间不会改变。它仍然会 be 14:00 in the new timezone. 在新时区是14:00。

Now, in GAS the epoch is "1/1/1970 0:00:00 GMT+0", now matter the timezone. 现在,在GAS中,时代是“1/1/1970 0:00:00 GMT + 0”,现在时区很重要。 So, the epoch is different and most likely the timezone too. 所以,时代是不同的,也很可能是时区。 This makes operating dates between GAS and the spreadsheet a total mess. 这使得GAS和电子表格之间的操作日期完全混乱。 To test the GAS epoch, do a range.setValue( new Date(0) ); 要测试GAS纪元,请执行range.setValue(new Date(0)); "

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

相关问题 根据电子表格中的值在自定义日期和时间触发基于 Google Apps 脚本时间的触发器 - Google Apps Script Time based trigger at custom date and time based on value in spreadsheet 日期时间编号中的Google Apps脚本时间 - Google Apps Script Time from Datetime number 使用日期选择器输入Google Apps脚本的电子表格日期 - Google Apps Script for Spreadsheet date input using date picker 如何在给定时间使用Apps脚本在Google Spreadsheet中“复制-粘贴值”动态数据? - How To Use an Apps Script To “Copy-PasteValue” Dynamic Data In a Google Spreadsheet at a given time? Google Apps 脚本:连接日期和时间 - Google Apps Script: Concatenating date and time 谷歌应用程序脚本 html 到电子表格 - Google apps script html to spreadsheet 如何判断Google Spreadsheet的Google Apps脚本中两个单元格是否相等 - How to tell if two cells are equal in Google Apps Script for Google Spreadsheet 如何使用Google Apps脚本自动下载Google电子表格数据 - how to automatically download Google spreadsheet data using Google Apps script 如何在 Google 电子表格的 Google Apps 脚本中解析 JSON - How to parse JSON in Google Apps Script for Google Spreadsheet Google Spreadsheet:如何使用Google Apps脚本命名范围 - Google Spreadsheet: How to name a range using Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM