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.

Create dates from vector

Have a look here for help with formatting strings.

After the strings are converted to Date type, we can, for example, easily find the difference in days.

Or, for example do the following.

lubridate package

We can use lubridate package to work with dates.

Then we can use functions like:

More info about the package is here.

Last updated

Was this helpful?