Date and Time
Date and Time
Date is represented by Date
class. Date is stored as number of days since 1970-01-01.
Time is represented by POSIXct
(represents date as large integer) or POSIXlt
(represents date as list of values) classes. Time is stored as number of seconds since 1970-01-01.
Dates
Get current time.
> date = date()
[1] "Mon Jan 5 10:54:17 2015"
> class(date)
[1] "character"
Get system date.
date = Sys.Date()
[1] "2015-01-05"
class(date)
[1] "Date"
Formatting and Parsing
We can use format function to format dates.
> date = Sys.Date()
> format(date, "%a %b %d")
[1] "Mon Jan 05"
> as.Date("1982-01-01")
[1] "1982-01-01"
time <- Sys.time()
as.POSIXlt(time)
as.POSIXct(time)
Create dates from vector
Have a look here for help with formatting strings.
> strings = c("05-01-2015", "06-01-2015")
> dates = as.Date(strings, "%d-%m-%Y")
> dates
[1] "2015-01-05" "2015-01-06"
After the strings are converted to Date type, we can, for example, easily find the difference in days.
> dates[2] - dates[1]
Time difference of 1 days
> as.numeric(dates[2] - dates[1])
[1] 1
Or, for example do the following.
> weekdays(date[1])
[1] "Monday"
> months(date[1])
[1] "January"
> julian(date[1])
[1] 16440
attr(,"origin")
[1] "1970-01-01"
lubridate package
We can use lubridate package to work with dates.
install.packages("lubridate")
library(lubridate)
Then we can use functions like:
> ymd("20150105")
[1] "2015-01-05 UTC"
> myd("05-2015-01")
[1] "2015-05-01 UTC"
> ymd_hms("2015-01-05 10:15:01")
[1] "2015-01-05 10:15:01 UTC"
> wday(ymd("20150105"))
[1] 2
More info about the package is here.
Last updated
Was this helpful?