Date
Sys.Date()
[1] "2015-02-15"
as.Date("01.11.1974", format="%d.%m.%Y")) (myDate <-
[1] "1974-11-01"
format(myDate, format="%d.%m.%Y")
[1] "01.11.1974"
as.Date(-374, "1910-12-16")) (negDate <-
[1] "1909-12-07"
as.numeric(negDate)
[1] -21940
POSIXct
Sys.time()
[1] "2015-02-15 13:24:25 CET"
date()
[1] "Sun Feb 15 13:24:25 2015"
as.POSIXct("2009-02-07 09:23:02")) (myTime <-
[1] "2009-02-07 09:23:02 CET"
ISOdate(2010, 6, 30, 17, 32, 10, tz="CET")
[1] "2010-06-30 17:32:10 CEST"
format(myTime, "%H:%M:%S")
[1] "09:23:02"
format(myTime, "%d.%m.%Y")
[1] "07.02.2009"
POSIXlt
c("05.08.1972, 03:37", "02.04.1981, 12:44")
charDates <- strptime(charDates, format="%d.%m.%Y, %H:%M")) (lDates <-
[1] "1972-08-05 03:37:00 CET" "1981-04-02 12:44:00 CEST"
$mday lDates
[1] 5 2
$hour lDates
[1] 3 12
weekdays(lDates)
[1] "Samstag" "Donnerstag"
months(lDates)
[1] "August" "April"
as.Date("01.11.1974", format="%d.%m.%Y")) (myDate <-
[1] "1974-11-01"
+ 365 myDate
[1] "1975-11-01"
as.Date("1976-06-19") - myDate) (diffDate <-
Time difference of 596 days
as.numeric(diffDate)
[1] 596
+ diffDate myDate
[1] "1976-06-19"
+ c(60, 120) lDates
[1] "1972-08-05 03:38:00 CET" "1981-04-02 12:46:00 CEST"
lDates[2] - lDates[1]) (diff21 <-
Time difference of 3162.338 days
1] + diff21 lDates[
[1] "1981-04-02 12:44:00 CEST"
seq(ISOdate(2010, 5, 1), ISOdate(2013, 5, 1), by="years")
[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"
seq(ISOdate(1997, 10, 22), by="2 weeks", length.out=4)
[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"
60 * 60 * 24
secsPerDay <- ISOdate(1995, 6, 13) + sample(0:(28*secsPerDay), 100, replace=TRUE)
randDates <- cut(randDates, breaks="week")
randWeeks <-summary(randWeeks)
1995-06-12 1995-06-19 1995-06-26 1995-07-03 1995-07-10
21 29 19 26 5
Packages timeDate
and lubridate
provide more functions for efficiently and consistently handling times and dates. More packages can be found in CRAN task view Time Series.
R markdown - markdown - R code - all posts