以周、月、季度和年为单位获取日期之间的差异 [英] Get the difference between dates in terms of weeks, months, quarters, and years

查看:34
本文介绍了以周、月、季度和年为单位获取日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日期,比如说 14.01.201326.03.2014.

I have two dates let´s say 14.01.2013 and 26.03.2014.

我想根据周(?)、月(在示例 14 中)、季度(4)和年(1)来计算这两个日期之间的差异.

I would like to get the difference between those two dates in terms of weeks(?), months(in the example 14), quarters(4) and years(1).

你知道获得这个的最好方法吗?

Do you know the best way to get this?

推荐答案

关于这个:

# get difference between dates `"01.12.2013"` and `"31.12.2013"`

# weeks
difftime(strptime("26.03.2014", format = "%d.%m.%Y"),
strptime("14.01.2013", format = "%d.%m.%Y"),units="weeks")
Time difference of 62.28571 weeks

# months
(as.yearmon(strptime("26.03.2014", format = "%d.%m.%Y"))-
as.yearmon(strptime("14.01.2013", format = "%d.%m.%Y")))*12
[1] 14

# quarters
(as.yearqtr(strptime("26.03.2014", format = "%d.%m.%Y"))-
as.yearqtr(strptime("14.01.2013", format = "%d.%m.%Y")))*4
[1] 4

# years
year(strptime("26.03.2014", format = "%d.%m.%Y"))-
year(strptime("14.01.2013", format = "%d.%m.%Y"))
[1] 1

as.yearmon()as.yearqtr() 位于 zoo 包中.year() 在包 lubridate 中.你怎么看?

as.yearmon() and as.yearqtr() are in package zoo. year() is in package lubridate. What do you think?

这篇关于以周、月、季度和年为单位获取日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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