将十进制日转换为 HH:MM [英] Convert decimal day to HH:MM
问题描述
我有一个十进制数向量,它代表十进制日",即一天的分数.我想使用 R 将其转换为 HH:MM 格式.
I have a vector of decimal numbers, which represent 'decimal day', the fraction of a day. I want to convert it into HH:MM format using R.
例如,数字 0.8541667
将对应于 20:30
.如何将数字转换为 HH:MM
格式?
For example, the number 0.8541667
would correspond to 20:30
. How can I convert the numbers to HH:MM
format?
推荐答案
试试这个:
R> format(as.POSIXct(Sys.Date() + 0.8541667), "%H:%M", tz="UTC")
[1] "20:30"
R>
我们从一个日期开始——它可以是任何日期,所以我们使用今天——然后加上你想要的小数天.
We start with a date--which can be any date, so we use today--and add your desired fractional day.
然后我们将 Date 类型转换为 Datetime 对象.
We then convert the Date type into a Datetime object.
最后,我们格式化 Datetime 对象的小时和分钟部分,确保时区使用 UTC.
Finally, we format the hour and minute part of the Datetime object, ensuring that UTC is used for the timezone.
这篇关于将十进制日转换为 HH:MM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!