R语言自定义函数获取上月最后一天
2021-09-22 本文已影响0人
lucier19981
获取上月最后一天日期
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) ) ) )