Dates and Times

In R: - Dates are stored internally as the number of days since 1970-01-01 - Tmes are stored internally as the number of seconds since 1970-01-01 - Dates are represented by the Date class - Times are represented by the POSIXct or the POSIXlt class

## I will not use Sys.time(), coz its not reproducible

x <- unclass(as.Date("2017-12-03"))

x <- unclass(as.Date("2017-11-03"))
# 17473

##  the number of years since 1970-01-01 
floor(unclass(x)/360) 
# 48

p <- as.POSIXlt("2018-1-01 05:20:30")
# "2018-01-01 05:20:30 CST"
names(unclass(p))
# [1] "sec"    "min"    "hour"   "mday"   "mon"    "year"   "wday"   "yday"  
#  [9] "isdst"  "zone"   "gmtoff"
p$min
# 20
p$sec
# 30
## read in date/time info in format 'm/d/y h:m:s'
dates <- c("02/27/02", "02/27/97", "01/14/08", "02/28/99", "02/01/95")
times <- c("23:03:20", "22:29:56", "11:03:30", "18:21:03", "06:56:26")
x <- paste(dates, times)
strptime(x, "%m/%d/%y %H:%M:%S",tz="UTC")
# [1] "2002-02-27 23:03:20 UTC" "1997-02-27 22:29:56 UTC" "2008-01-14 11:03:30 UTC"
# [4] "1999-02-28 18:21:03 UTC" "1995-02-01 06:56:26 UTC"
lct <- Sys.getlocale("LC_TIME")
lct
# [1] "Chinese (Simplified)_People's Republic of China.936"

Sys.setlocale("LC_TIME", "C")
x <- c("1jan1960", "2jul1970", "31mar2010", "30jul1990")
z <- strptime(x, "%d%b%Y")
z
# "1960-01-01 CST" "1970-07-02 CST" "2010-03-31 CST" "1990-07-30 CST"

Sys.setlocale("LC_TIME", lct)
x <- c("1一月1960", "2十月1970", "31八月2010", "30jul1990")
z <- strptime(x, "%d%b%Y")
z
# "1960-01-01 CST" "1970-10-02 CST" "2010-08-31 CST" NA 

date()
# "Thu Apr 19 13:22:28 2018"
## locale-specific
months(p); weekdays(p)
# [1] "一月"
# [1] "星期一"
format(Sys.time(), "%a %b %d %X %Y %Z %H:%M:%OS3")
# "周四 四月 19 13:24:01 2018 CST 13:24:01.565"
x <- as.Date("2018-04-01")
y <- as.Date("2017-02-28")
x-y
# Time difference of 397 days
x <- as.POSIXct("2017-10-01 05:00:00")
y <- as.POSIXct("2017-10-02 01:00:00", tz = "GMT")
y-x
# Time difference of 1.166667 days
cols <- c("code", "coordinates", "TZ", "comments")
tmp <- read.delim(file.path(R.home("share"), "zoneinfo", "zone.tab"),
                  header = FALSE, comment.char = "#", col.names = cols)
knitr::kable(head(tmp, 20))
code coordinates TZ comments
AD +4230+00131 Europe/Andorra
AE +2518+05518 Asia/Dubai
AF +3431+06912 Asia/Kabul
AG +1703-06148 America/Antigua
AI +1812-06304 America/Anguilla
AL +4120+01950 Europe/Tirane
AM +4011+04430 Asia/Yerevan
AO -0848+01314 Africa/Luanda
AQ -7750+16636 Antarctica/McMurdo New Zealand time - McMurdo, South Pole
AQ -6617+11031 Antarctica/Casey Casey
AQ -6835+07758 Antarctica/Davis Davis
AQ -6640+14001 Antarctica/DumontDUrville Dumont-d’Urville
AQ -6736+06253 Antarctica/Mawson Mawson
AQ -6448-06406 Antarctica/Palmer Palmer
AQ -6734-06808 Antarctica/Rothera Rothera
AQ -690022+0393524 Antarctica/Syowa Syowa
AQ -720041+0023206 Antarctica/Troll Troll
AQ -7824+10654 Antarctica/Vostok Vostok
AR -3436-05827 America/Argentina/Buenos_Aires Buenos Aires (BA, CF)
AR -3124-06411 America/Argentina/Cordoba Argentina (most areas: CB, CC, CN, ER, FM, MN, SE, SF)

Lubridate

For More, Using library(lubridate):