从R中的当前日期获取上个月的开始日期和结束日期 [英] Getting previous month start date and end date from current date in R

查看:201
本文介绍了从R中的当前日期获取上个月的开始日期和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何简单的方法可以从R中的当前日期获取上个月的开始日期和结束日期?

Is there any easy way for getting start date and end date of previous month from the current date in R?

我只有当前日期。通过它,我想获取上个月,上个月的开始日期,上个月的结束日期。

I have only the current date. From it, i want to get the previous month, start date of previous month, end date of previous month.

currentDate<-Sys.Date() #return today's date as "2012-11-07"

我想要上一个从今天开始的月份开始日期为2012-10-01,结束日期为2012-10-31。

I want the previous month start date as 2012-10-01 and end date as 2012-10-31 from today's date.

推荐答案

许多软件包都具有方便的日期功能,但是要自己动手:

A number of packages have handy date functions, but to roll your own:

月初功能:

som <- function(x) {
  as.Date(format(x, "%Y-%m-01"))
}

和月末函数(尽管这里不需要):

and an end of month function (although you won't need this here):

eom <- function(x) {
  som(som(x) + 35) - 1
}

那应该让你走。例如,要获取上个月的月末:

That should get you going. For example, to get the end of the previous month:

som(Sys.Date()) - 1
[1] "2012-10-31"

和月初:

som(som(Sys.Date()) - 1)
[1] "2012-10-01"

这篇关于从R中的当前日期获取上个月的开始日期和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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