R语言自定义函数获取上月最后一天

获取上月最后一天日期

udf_pre_max_d <- function(date)
{
year = year(date) # 当前年
month = month(date) # 当前月
beforeYear = 0
beforeMonth = 0
if (month <= 1) # 如果当前月是一月,那么年份就要减1, 上个月为12月
{
beforeYear = year - 1;
beforeMonth =12
} else
{
beforeYear = year
beforeMonth = month - 1 # 上个月
}
as.Date(paste0(beforeYear,"-" , beforeMonth ,"-" ,
udf_mdays( as.Date(paste0(beforeYear,"-",beforeMonth,"-","1")) ) ) )
}

上月及上上月最后一天

Date_last_month_day <-
ymd(paste0(Date_year,"-",
month(ymd(paste0(Date_year,Date_labs)) -months(1)) ,"-",
days_in_month(ymd(paste0(Date_year,Date_labs))-months(1)) ) )

Date_last_month_day1 <-
ymd(paste0(Date_year,"-",
month(ymd(paste0(Date_year,Date_labs)) - days(62) ) ,"-",
days_in_month(ymd(paste0(Date_year,Date_labs)) - days(62) ) ) )

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容