是否可以使用HH:MM:SS格式打印持续时间? [英] Is it possible to print a duration with HH:MM:SS format?

查看:60
本文介绍了是否可以使用HH:MM:SS格式打印持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

t1<-as.POSIXct("2017-03-02 11:58:20")
t2<-as.POSIXct("2017-03-02 12:00:05")
print(lubridate::as.duration(lubridate::interval(t1,t2)))

[1]"105s(〜1.75分钟)"

[1] "105s (~1.75 minutes)"

是否可以用HH:MM:SS表示持续时间?因此,在这种情况下将是:

Is it possible to have the duration expressed in HH:MM:SS? So in this case it would be:

00:01:45

00:01:45

推荐答案

您可以将 lubridate 中的 seconds_to_period sprintf

library(lubridate)
td <- seconds_to_period(difftime(t2, t1, units = "secs"))
sprintf('%02d:%02d:%02d', td@hour, minute(td), second(td))
#[1] "00:01:45"

这是基于先前的讨论-将秒转换为天:小时:minutes:r的秒数?

This is based on an earlier discussion - Converting seconds to days: hours:minutes:seconds in r?

这篇关于是否可以使用HH:MM:SS格式打印持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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