将十进制天转换为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屋!