R Date and Time

R provides very large range of capabilities to deal with times and dates. Generally, dates are internally stored as integers, and depending on the operations we will need, we could choose to do the same. R provides three date/time classes:

Date Class

This is the simplest data type. It handles with the date but does not deal with the times. The default format is “yyyy-mm-dd”. Internally Date object are stored as the number of days since 01-01-1970, using negative numbers for earlier dates.

Formats:

Code

Value

%d

Day of the month (decimal number)

%m

Month (decimal number)

%b

Month (abbreviated)

%B

Month (full name)

%y

Year (2 digit)

%Y

Year (4 digit)


Example 1:

#create a date
d <- as.Date("2019-03-11")
d

Output:

[1]  2019-03-11

If you are specifying non-standard format you must have to use format = argument.

Example 2:

# non –standard format must be specified
> d <- as.Date("03/11/2019", format = "%m/%d/%Y")
> d

Output:

[1]  2019-03-11

Example 3:

> d <- as.Date("March 11, 2019", format = "%B %d, %Y")
> d

Output:

[1] "2019-03-11"

Example 4:

Find the difference between current system date and specified date:

# take a difference
> Sys.Date() - as.Date("1970-01-01")

Output:

Time difference of 9157 days

Example 5:

> difftime(Sys.Date(), as.Date("1994-02-13"), units = "weeks")

Here, units argument can be “auto”, “secs”, “mins”, “hours”, “days”, “weeks”.

Output:

Time difference of 1308.143 weeks

Example 6:

Let's create a sequence of dates:

six.weeks <- seq(d, length = 6, by = "week")
six.weeks

Output:

[1] "2019-03-11" "2019-03-18" "2019-03-25" "2019-04-01" "2019-04-08" "2019-04-15"


POSIX

POSIX represents a portable operating system interface, primarily for UNIX, but available on another operating systems as well. Dates stored in the POSIX format are date/time values and also allows modification of time zone.

There are two POSIX types, i.e. POSIXct and POSIXlt. “ct” stands for calendar time, it stores the number of seconds since the origin (1 January, 1970). Negative numbers represents the number of seconds before this time and positive number represent the number of seconds afterwards.

And “lt” stands for local time. It keeps the date as a list of time attributes (such as “hour” and “mon”).

Code

Meaning

Code

Meaning

%a

Abbreviated weekday

%A

Full weekday

%b

Abbreviated month

%B

Full month

%c

Locale-specific date and time

%d

Decimal date

%H

Decimal hours (24 hour)

%I

Decimal hours (12 hour)

%j

Decimal day of the year

%m

Decimal month

%M

Decimal minute

%p

Locale-specific AM/PM

%S

Decimal second

%U

Decimal week of the year (starting on Sunday)

%w

Decimal Weekday (0=Sunday)

%W

Decimal week of the year (starting on Monday)

%x

Locale-specific Date

%X

Locale-specific Time

%y

2-digit year

%Y

4-digit year

%z

Offset from GMT

%Z

Time zone (character)


Example 1:

tm1 <- as.POSIXct("2019-03-11 23:55:26")
tm1

Output:

[1] "2019-03-11 23:55:26 IST"

Example 2:

tm2 <- as.POSIXct("24032019 08:32:07", format = "%d%m%Y %H:%M:%S")
tm2

Output:

[1] "2019-03-24 08:32:07 IST"

Example 3:

Specify the time zone:

tm3 <- as.POSIXct("1989-06-24 11:42:03", tz = "GMT")
tm3

Output:

[1] "1989-06-24 11:42:03 GMT"

Example 4:

Let's see some example of POSIXlt:

tm1.lt <- as.POSIXlt("1989-06-24 23:55:26")
tm1.lt

Output:

[1] "1989-06-24 23:55:26 IST"

Example 5:

unclass(tm1.lt)

Output:

$sec

[1] 26

$min

[1] 55

$hour

[1] 23

$mday

[1] 24

$mon

[1] 5

$year

[1] 89

$wday

[1] 6

$yday

[1] 174

$isdst

[1] 0

$zone

[1] "IST"

$gmtoff

[1] NA

Example 6:

unlist(tm1.lt)

Output:

sec    min   hour   mday    mon   year   wday   yday  isdst   zone   gmtoff
"26"   "55"   "23"   "24"     "5"     "89"    "6"     "174"    "0"   "IST"     NA


Chron Package

Chron is short for chronological. This package is good option when you don’t need to deal with time zones.

Chron is a package and it is not a base package, you will have to install it.

Example:

tm1.c <- as.chron("2019-06-24 23:55:26")
tm1.c

Output:

[1] (06/24/19 23:55:26)

Example 2:

tm2.c <- as.chron("06/24/13 08:32:07", "%m/%d/%y %H:%M:%S")
tm2.c

Output:

[1] (06/24/19 08:32:07)
Reference: https://www.stat.berkeley.edu/~s133/dates.html