excel - 我应该如何在 VBA 中以分钟为单位获得时间
问题描述
我在 VBA 中存储大量数据,然后在 excel 中导出该文件。如果时间大于一小时,我可以收集时间,但如果小于 59 分钟,我无法收集时间。我正在使用类似的函数来计算时差
- // 获取时间差 00:00
- var time = TIMEDIFF('00:00', $inspection_time);
- // 比如检查时间是12:00pm,结果应该是0.5
- 变率=时间/24;
- SETRESULT(率);
解决方案
First off, TIMEDIFF
is a function I did not immediately find documentation for.
Also, time/dates are usually represented in terms of days.
To get hours from that you would multiply by 24 to get hours (then 60 for minutes, etc., etc.), not divide.
Using an Existing Function
A simple way to calculate time differences using a built in function is with DateDiff
which has a signature of:
DateDiff( interval, date1, date2, [firstdayofweek], [firstweekofyear] )
There are plenty of intervals for you to choose from, ranging from year to second. Minutes is "n".
Ex:
DateDiff("n", "00:00", "12:00") '-> Result: 0.5
Documentation: msdn.microsoft.com/.../datediff-function
Simple Math Version
Even simpler is to just subtract:
abs(time2 - time1)
Where the result will be in days.
EDIT:
I would encourage you to make sure your functions do exactly what you expect them to do and not just hack something together that works but upon further changes breaks. It's poor coding practice and you learn nothing from it.
However, in an effort to give a response, a simple roundabout way to get a minimum cutoff of one hour would be to simply take the smallest value you would like to have, compare it to your value and choose the larger of the two.
calculatedValue <- SomeNumber
cutoffValue <- 1/24
chosenValue <- MAX(calculatedValue, cutoffValue)
推荐阅读
- java - 在ubuntu中工作'java -cp'突然不工作了
- excel - 从美国格式转换为英国格式
- c++ - 从代码协商 gstreamer 管道时出错
- mysql - 识别购买超过 1 次的产品
- python - Plotly Dash 应用部署在 Hereko 上,应用在本地加载需要 6 分钟
- angular - 创建了新组件,但我在模块中收到有关提供程序的错误
- java - 如何使用 Java 为 GET https 请求下载 zip 格式的响应文件。在邮递员中,响应是一些垃圾字符
- python - 使用 Bash 编辑 YAML 文件
- python - 将 XML 文件转换为 Python 中的列表列表
- flutter - Flutter 在按钮点击时创建新的类实例并更新值