如何使用脚本将时间转换为Google表格中的十进制浮点数? [英] How to convert Time into decimal float in Google Sheets using Script?

查看:61
本文介绍了如何使用脚本将时间转换为Google表格中的十进制浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将时间 HH:MM 转换为 H.xx

就像我以这种格式获取它一样: Sat Dec 30 00:00:00 GMT + 05:21 1899

Like I am getting it in this format: Sat Dec 30 00:00:00 GMT+05:21 1899

但是此值在单元格中为 04:29 .我希望它是 4.5 小时,才能将其乘以小时费率.

But this value is 04:29 in cell. I want it to be 4.5 hours to multiply it to hourly rate.

推荐答案

Google表格

在Google表格中,如果您在单元格中有日期/时间值(例如"D9"),请使用 = HOUR(D9)+(MINUTE(D9)/60).

如果该值以 04:29 格式存储,则使用 = INDEX(SPLIT(D9,:"),1)+(INDEX(SPLIT(D9,":),2)/60).

If the value is stored in the format 04:29, then use =INDEX(SPLIT(D9, ":"), 1) + (INDEX(SPLIT(D9, ":"), 2)/60).

如果要使用Google Sheets API或Google Apps脚本,则可以使用javascript.

If you want to use the Google Sheets API or Google Apps Script, then you can use javascript.

您需要使用 getMinutes()方法除以60,然后添加到小时(使用 getHours()).

You need to use the getMinutes() method and divide by 60, then add that to the hour (using getHours()).

var date = new Date();
var minutes = date.getMinutes();
var output = date.getHours() + (minutes/60);

请注意,这忽略了秒.

如果单元格中的值存储为类似于 04:29 的字符串,则您需要

If the value in the cell is stored as a string like 04:29, then you'll need to split it.

var time = "04:29";
var hour = Number(time.split(":")[0]);
var minutes = Number(time.split(":")[1]);
var output = hour + (minutes/60);

这篇关于如何使用脚本将时间转换为Google表格中的十进制浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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