Introduction to Date and Time in R

I. Ozkan

2025-08-20

References

Before Start: Videos

Date and Times - 1: R. Peng

Representing Times - 2: R. Peng

Base Functions: Date in R

Sys.Date()  
format.Date() # simply format()
weekdays()
seq.Date()
as.Date() 

Sys.Date()

# Current Date and Time
Sys.Date()
## [1] "2025-08-20"
Sys.time()
## [1] "2025-08-20 10:23:56 +03"
today <- Sys.Date()

today 
## [1] "2025-08-20"

Base Functions: Date in R

format.Date() # simply format()
weekdays()
seq.Date()
as.Date() 

format()

today 
## [1] "2025-08-20"
format(today, "%d %m %y") 
## [1] "20 08 25"
format.Date(today, "%d %m %y")
## [1] "20 08 25"
format(today, "%d %m %y") 
## [1] "20 08 25"
format.Date(today, "%d/%m/%y")
## [1] "20/08/25"
format.Date(today, "%d-%m-%y")
## [1] "20-08-25"

Base Functions: Date in R

format.Date() # simply format()
weekdays()
seq.Date()
as.Date() 

format()

Code Value
%d Day of month as number
%m Month as number
%b Month abbreviated
%B Month full name
%y Year, two digits
%Y Year, four digits
format(today, "%d %b %y")  # with month abbreviated   
## [1] "20 Aug 25"
format(today, "%d %B %y")  # with month full name   
## [1] "20 August 25"
format(today, "%d %B %Y")  # with month full name   
## [1] "20 August 2025"

Base Functions: Date in R

as.Date() 
weekdays()
seq.Date()

as.Date() Function

as.Date(c("04-28-2015", "11-12-2016"), format = "%m-%d-%Y")
## [1] "2015-04-28" "2016-11-12"
x <- as.Date(c("04-28-2015", "11-12-2016"), format = "%m-%d-%Y")
str(x)
##  Date[1:2], format: "2015-04-28" "2016-11-12"
format(x, "%d %B %Y")
## [1] "28 April 2015"    "12 November 2016"
format(x, "%B %d, %Y")
## [1] "April 28, 2015"    "November 12, 2016"

Base Functions: Date in R

as.Date() 
weekdays()
seq.Date()

as.Date() Function

as.Date(c("07/14/17"), format = "%m/%d/%y")
## [1] "2017-07-14"
x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
x
## [1] "1jan1960"  "2jan1960"  "31mar1960" "30jul1960"
str(x)
##  chr [1:4] "1jan1960" "2jan1960" "31mar1960" "30jul1960"
z <- as.Date(x, "%d%b%Y")
z
## [1] "1960-01-01" "1960-01-02" "1960-03-31" "1960-07-30"
str(z)
##  Date[1:4], format: "1960-01-01" "1960-01-02" "1960-03-31" "1960-07-30"

Base Functions: Date in R

as.Date() 
weekdays()
seq.Date()

strptime() Function

# number of days since origin time.. 
as.Date(32768, origin = "1900-01-01") # number of days since 1900
## [1] "1989-09-19"
# a complex one - with time
datestring <- c("January 10, 2012 10:40", "December 9, 2011 9:10")
datestring
## [1] "January 10, 2012 10:40" "December 9, 2011 9:10"
# strptime() function
(datestring_d <- strptime(datestring, "%B %d, %Y %H:%M"))
## [1] "2012-01-10 10:40:00 EET" "2011-12-09 09:10:00 EET"
str(datestring)
##  chr [1:2] "January 10, 2012 10:40" "December 9, 2011 9:10"
str(datestring_d)
##  POSIXlt[1:2], format: "2012-01-10 10:40:00" "2011-12-09 09:10:00"
as.Date(datestring_d)
## [1] "2012-01-10" "2011-12-09"

Base Functions: Date in R

weekdays()
seq.Date()

weekdays() Function

Sys.Date()
## [1] "2025-08-20"
weekdays(Sys.Date())
## [1] "Wednesday"
weekdays(Sys.Date() + 1:10)
##  [1] "Thursday"  "Friday"    "Saturday"  "Sunday"    "Monday"    "Tuesday"  
##  [7] "Wednesday" "Thursday"  "Friday"    "Saturday"
z
## [1] "1960-01-01" "1960-01-02" "1960-03-31" "1960-07-30"
weekdays(z)
## [1] "Friday"   "Saturday" "Thursday" "Saturday"
z[weekdays(z)=="Saturday"]
## [1] "1960-01-02" "1960-07-30"
z[weekdays(z) %in% c("Saturday", "Friday")]
## [1] "1960-01-01" "1960-01-02" "1960-07-30"

Base Functions: Date in R

seq.Date()

seq.Date() Function

# by= one of the ("day", "week", "month", "quarter" or "year")

seq.Date(from=as.Date("2023/1/1"), to=as.Date("2025/1/1"), by="quarter")
## [1] "2023-01-01" "2023-04-01" "2023-07-01" "2023-10-01" "2024-01-01"
## [6] "2024-04-01" "2024-07-01" "2024-10-01" "2025-01-01"
seq.Date(from=as.Date("2023/1/1"), to=as.Date("2023/4/1"), by="month")
## [1] "2023-01-01" "2023-02-01" "2023-03-01" "2023-04-01"
seq.Date(from=as.Date("2023/4/1"), to=as.Date("2023/1/1"), by="-1 month")
## [1] "2023-04-01" "2023-03-01" "2023-02-01" "2023-01-01"
# length.out = number
seq.Date(from=as.Date("2020/1/1"), length.out = 5, by="quarter")
## [1] "2020-01-01" "2020-04-01" "2020-07-01" "2020-10-01" "2021-01-01"
# both by and length.out
seq.Date(from=as.Date("2020/1/1"), length.out = 5, by="month")
## [1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01"

Base Functions: Date in R

seq.Date()

seq.Date() Function

(x <- as.Date("1jan1960", format = "%d%b%Y"))
## [1] "1960-01-01"
seq(from = x, length = 6, by = "week")
## [1] "1960-01-01" "1960-01-08" "1960-01-15" "1960-01-22" "1960-01-29"
## [6] "1960-02-05"
weekdays(seq(from = x, length = 6, by = "week"))  
## [1] "Friday" "Friday" "Friday" "Friday" "Friday" "Friday"
weekdays(seq(from = x, length = 6, by = "day"))  
## [1] "Friday"    "Saturday"  "Sunday"    "Monday"    "Tuesday"   "Wednesday"
weekdays(seq(from = x, length = 6, by = "month"))  
## [1] "Friday"    "Monday"    "Tuesday"   "Friday"    "Sunday"    "Wednesday"
# change to numeric
as.numeric(x)
## [1] -3653
# teaser, subtraction
x - as.Date("1970-01-01")
## Time difference of -3653 days

Basic Operations: Subtraction, Addition and Comparison

x <- as.Date("May 05, 2023", format = "%B %d, %Y")
x
## [1] "2023-05-05"
y <- as.Date("March 5, 2023", format = "%B %d, %Y")
y
## [1] "2023-03-05"
# subtraction: number of days 
x - y
## Time difference of 61 days
y - x
## Time difference of -61 days
x - seq(from = y, length = 6, by = "week")
## Time differences in days
## [1] 61 54 47 40 33 26
# adding and subtracting number
x + 1
## [1] "2023-05-06"
x - 1
## [1] "2023-05-04"
# comparisons 
x > y
## [1] TRUE
y > x
## [1] FALSE

ts Objects

ts(data = NA, start = 1, end = numeric(), frequency = 1,
   deltat = 1, ts.eps = getOption("ts.eps"), class = , names = ) 

as.ts()  
is.ts()

ts Object Examples

first_ts <- ts(1:10, frequency = 4, start = c(1959, 2))
first_ts
##      Qtr1 Qtr2 Qtr3 Qtr4
## 1959         1    2    3
## 1960    4    5    6    7
## 1961    8    9   10
print(first_ts)
##      Qtr1 Qtr2 Qtr3 Qtr4
## 1959         1    2    3
## 1960    4    5    6    7
## 1961    8    9   10
print(first_ts, calendar = F)
## Time Series:
## Start = c(1959, 2) 
## End = c(1961, 3) 
## Frequency = 4 
##  [1]  1  2  3  4  5  6  7  8  9 10
# column wise
matrix(data=1:12,nrow=3)
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
# row wise
matrix(data=1:12,nrow=3, byrow = TRUE)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   12
# without column names
ts(matrix(1:20, nrow = 5), start = c(1961, 1), frequency = 12)
##          Series 1 Series 2 Series 3 Series 4
## Jan 1961        1        6       11       16
## Feb 1961        2        7       12       17
## Mar 1961        3        8       13       18
## Apr 1961        4        9       14       19
## May 1961        5       10       15       20
# with column names
second_ts <- ts(matrix(1:20, 5), start = c(1961, 1), frequency = 12,
   names = c("x_1","x_2","x_3","x_4"))

second_ts
##          x_1 x_2 x_3 x_4
## Jan 1961   1   6  11  16
## Feb 1961   2   7  12  17
## Mar 1961   3   8  13  18
## Apr 1961   4   9  14  19
## May 1961   5  10  15  20
is.ts(second_ts)
## [1] TRUE
is.ts(first_ts)
## [1] TRUE

Useful Functions for ts Objects

tsp() 
frequency()  
start()
end()
time()
window()
plot.ts() # only example 

tsp(first_ts)
## [1] 1959.25 1961.50    4.00
# first: 1959.25, second: 1961.50 freq: 4.00

tsp(second_ts)
## [1] 1961.000 1961.333   12.000
frequency(first_ts)
## [1] 4
frequency(second_ts)
## [1] 12
time(second_ts)
##           Jan      Feb      Mar      Apr      May
## 1961 1961.000 1961.083 1961.167 1961.250 1961.333
time(first_ts)
##         Qtr1    Qtr2    Qtr3    Qtr4
## 1959         1959.25 1959.50 1959.75
## 1960 1960.00 1960.25 1960.50 1960.75
## 1961 1961.00 1961.25 1961.50
# windowing 
window(second_ts, start=c(1961,2)) 
##          x_1 x_2 x_3 x_4
## Feb 1961   2   7  12  17
## Mar 1961   3   8  13  18
## Apr 1961   4   9  14  19
## May 1961   5  10  15  20
window(second_ts, start=c(1961,2), end=c(1961,6)) 
##          x_1 x_2 x_3 x_4
## Feb 1961   2   7  12  17
## Mar 1961   3   8  13  18
## Apr 1961   4   9  14  19
## May 1961   5  10  15  20
# extend the time series
window(second_ts, start=c(1961,2), end=c(1962,5), extend=TRUE) 
##          x_1 x_2 x_3 x_4
## Feb 1961   2   7  12  17
## Mar 1961   3   8  13  18
## Apr 1961   4   9  14  19
## May 1961   5  10  15  20
## Jun 1961  NA  NA  NA  NA
## Jul 1961  NA  NA  NA  NA
## Aug 1961  NA  NA  NA  NA
## Sep 1961  NA  NA  NA  NA
## Oct 1961  NA  NA  NA  NA
## Nov 1961  NA  NA  NA  NA
## Dec 1961  NA  NA  NA  NA
## Jan 1962  NA  NA  NA  NA
## Feb 1962  NA  NA  NA  NA
## Mar 1962  NA  NA  NA  NA
## Apr 1962  NA  NA  NA  NA
## May 1962  NA  NA  NA  NA
window(first_ts, start=c(1960,1), end=c(1961,1))
##      Qtr1 Qtr2 Qtr3 Qtr4
## 1960    4    5    6    7
## 1961    8
# modify the values 
window(first_ts, start=c(1960,1), end=c(1960,3)) <- NA

first_ts
##      Qtr1 Qtr2 Qtr3 Qtr4
## 1959         1    2    3
## 1960   NA   NA   NA    7
## 1961    8    9   10
plot(first_ts)

plot(second_ts)

plot(second_ts, plot.type="single", lty=1:4, xlab="X", ylab="Y",
     main="My First Multi-variate Time Series Plot")

Time in R

POSIXct Class

Sys.timezone()
## [1] "Europe/Istanbul"
x <- Sys.Date()
str(x)
##  Date[1:1], format: "2025-08-20"
x_1 <- as.POSIXct(x)
x_1
## [1] "2025-08-20 UTC"
x <- Sys.time()
str(x)
##  POSIXct[1:1], format: "2025-08-20 10:23:56"
x_p <- as.POSIXlt(x)
str(x_p)
##  POSIXlt[1:1], format: "2025-08-20 10:23:56"
names(unclass(x_p))
##  [1] "sec"    "min"    "hour"   "mday"   "mon"    "year"   "wday"   "yday"  
##  [9] "isdst"  "zone"   "gmtoff"
x_p$sec
## [1] 56.92476
x_p$hour
## [1] 10

An Important Package: lubridate Package