Change day of the month in a Date to first day (01)

前端 未结 5 769
别那么骄傲
别那么骄傲 2020-11-30 11:10

I want to set the day of month in a Date to start date of current month (01). Now I use the following:

currentDate <- Sys.Date()         


        
相关标签:
5条回答
  • 2020-11-30 11:34

    Yes, there is a one-liner for that using function cut:

    cut(Sys.Date(), "month")
    [1] 2012-11-01
    Levels: 2012-11-01
    
    0 讨论(0)
  • 2020-11-30 11:44
    as.Date(paste0(format(Sys.Date(), "%Y-%m"), "-01"))
    
    0 讨论(0)
  • 2020-11-30 11:46

    lubridate's floor_date keeps it a Date

    library(lubridate)
    
    floor_date(Sys.Date(), "month")
    
    0 讨论(0)
  • 2020-11-30 11:48

    Also one line using format function:

    format(Sys.Date(),"01-%m-%Y")
    [1] "01-06-2017"
    
    0 讨论(0)
  • 2020-11-30 11:49

    Using lubridate (also work with sec, min, hour, mon, year etc.)

    library(lubridate)
    
    dt <- Sys.Date()
    day(dt) <- 01
    
    dt
    # [1] "2018-01-01"
    
    0 讨论(0)
提交回复
热议问题