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