美文网首页
R语言自定义函数获取上月最后一天

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

作者: lucier19981 | 来源:发表于2021-09-22 08:33 被阅读0次

    获取上月最后一天日期

    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) ) ) )

    相关文章

      网友评论

          本文标题:R语言自定义函数获取上月最后一天

          本文链接:https://www.haomeiwen.com/subject/syewgltx.html