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()
Yes, there is a one-liner for that using function cut
:
cut(Sys.Date(), "month")
[1] 2012-11-01
Levels: 2012-11-01
as.Date(paste0(format(Sys.Date(), "%Y-%m"), "-01"))
lubridate
's floor_date
keeps it a Date
library(lubridate)
floor_date(Sys.Date(), "month")
Also one line using format function:
format(Sys.Date(),"01-%m-%Y")
[1] "01-06-2017"
Using lubridate
(also work with sec
, min
, hour
, mon
, year
etc.)
library(lubridate)
dt <- Sys.Date()
day(dt) <- 01
dt
# [1] "2018-01-01"