Time and date

Create and format date values

Class Date

[1] "2020-12-08"
[1] "1974-11-01"
[1] "01.11.1974"

Internal representation

[1] "1909-12-07"
[1] -21940

Time values

Class POSIXct

[1] "2020-12-08 16:32:22 CET"
[1] "Tue Dec  8 16:32:22 2020"
[1] "2009-02-07 09:23:02 CET"
[1] "2010-06-30 17:32:10 CEST"
[1] "09:23:02"
[1] "07.02.2009"

Class POSIXlt

[1] "1972-08-05 03:37:00 CET"  "1981-04-02 12:44:00 CEST"
[1] 5 2
[1]  3 12
[1] "Saturday" "Thursday"
[1] "August" "April" 

Time and date arithmetic

Sum and difference of time-date values

[1] "1974-11-01"
[1] "1975-11-01"
Time difference of 596 days
[1] 596
[1] "1976-06-19"
[1] "1972-08-05 03:38:00 CET"  "1981-04-02 12:46:00 CEST"
Time difference of 3162.338 days
[1] "1981-04-02 12:44:00 CEST"
[1] "1972-08-05 CET"  "1981-04-03 CEST"
[1] "1972-01-01 CET" "1981-01-01 CET"

Systematically and randomly generate time-date values

[1] "2010-05-01 12:00:00 GMT" "2011-05-01 12:00:00 GMT"
[3] "2012-05-01 12:00:00 GMT" "2013-05-01 12:00:00 GMT"
[1] "1997-10-22 12:00:00 GMT" "1997-11-05 12:00:00 GMT"
[3] "1997-11-19 12:00:00 GMT" "1997-12-03 12:00:00 GMT"
1995-06-12 1995-06-19 1995-06-26 1995-07-03 1995-07-10 
        19         25         29         24          3 

Useful packages

Package lubridate provides many functions for efficiently and consistently handling times and dates. More packages can be found in CRAN task view Time Series.

Get the article source from GitHub

R markdown - markdown - R code - all posts