ZetCode

R dates and times

last modified November 19, 2026

R has three built-in classes for temporal data: Date for a calendar day, POSIXct for a moment in time stored as seconds, and POSIXlt for the same moment stored as a list of components such as year, month, and hour. A Date carries no time zone, while the POSIX classes do, which is the source of most confusion around dates. This article shows how to create, format, parse, compare, and calculate with dates, and how to move between the three classes.

Current date and time

Sys.Date returns today as a Date, and Sys.time returns the current instant as a POSIXct. Their values depend on the clock, so the next script prints only the classes and one structural fact; an output block that contained the current time would be wrong one minute later.

current.R
today <- Sys.Date()
now <- Sys.time()
print(class(today))
print(class(now))
cat("Date stores days:", is.numeric(unclass(today)), "\n")

A Date is stored as a single number, which the last line confirms. A date counts the days since the first of January 1970, and a POSIXct value counts the seconds since the same moment. The printed forms are just formatted versions of those numbers, which is why dates sort correctly and can be added like numbers.

$ Rscript current.R
[1] "Date"
[1] "POSIXct" "POSIXt" 
Date stores days: TRUE 

Creating dates

as.Date converts text to a Date. Without a format it expects the ISO order year-month-day, which is unambiguous and therefore the safest form for data files. For other orders, the format argument describes where the day, the month, and the year appear.

creating.R
d <- as.Date("2026-11-19")
print(d)
print(class(d))
print(unclass(d))
print(as.Date("19/11/2026", format = "%d/%m/%Y"))

The printed date looks like the input, but the stored value is the integer 20776. The last line reads the European order day/month/year through the pattern %d/%m/%Y. Without that argument R would try the ISO order and return NA, so a date that arrives in another order must always be parsed with an explicit format.

$ Rscript creating.R
[1] "2026-11-19"
[1] "Date"
[1] 20776
[1] "2026-11-19"

Formatting dates

format turns a date into text according to a pattern. The conversion specifications are the same ones that strptime uses for parsing, so the two functions are mirror images. %d is the day, %m the month number, %B the full month name, %A the weekday name, and %Y the four-digit year.

formatting.R
d <- as.Date("2026-11-19")
print(format(d, "%d %B %Y"))
print(format(d, "%A, %d %B %Y"))
print(format(d, "%Y-%m-%d"))
print(format(d, "%d/%m/%y"))

The same date appears as a long English form, a full weekday form, the ISO form, and a short numeric form. Month and weekday names follow the locale of the session, which is English in the examples. Code that parses such names later should set the locale explicitly or avoid names altogether.

$ Rscript formatting.R
[1] "19 November 2026"
[1] "Thursday, 19 November 2026"
[1] "2026-11-19"
[1] "19/11/26"

Parts of a date

A Date has no fields to read directly, so its parts are extracted with format and the matching conversion. The functions weekdays, months, and quarters return the common labels without a format string. %j is the day of the year, counted from one.

components.R
d <- as.Date("2026-11-19")
print(format(d, "%Y"))
print(format(d, "%m"))
print(format(d, "%d"))
print(format(d, "%j"))
print(weekdays(d))
print(months(d))
print(quarters(d))

The date falls in 2026, in month 11, on day 19, which is day 323 of the year. It is a Thursday in November and in the fourth quarter. Formatted components are character strings; convert them with as.integer before doing arithmetic on them.

$ Rscript components.R
[1] "2026"
[1] "11"
[1] "19"
[1] "323"
[1] "Thursday"
[1] "November"
[1] "Q4"

Date arithmetic

Adding a whole number to a Date adds that many days, because the underlying value counts days. Subtracting two dates returns a difftime object, which prints as a time difference and can be converted to a chosen unit. Months and years are not fixed lengths, so they need the functions described in the documentation rather than addition.

arithmetic.R
start <- as.Date("2026-11-19")
deadline <- start + 30
print(deadline)
print(deadline - start)
print(difftime(deadline, start, units = "weeks"))

Thirty days after the nineteenth of November is the nineteenth of December. The difference is thirty days, which is about 4.29 weeks. difftime does not change the stored difference; it only chooses the unit in which the value is displayed and returned.

$ Rscript arithmetic.R
[1] "2026-12-19"
Time difference of 30 days
Time difference of 4.285714 weeks

Date and time together

as.POSIXct creates a timestamp from text and a time zone. The result prints with the zone abbreviation, and format can show the offset or the zone name. as.POSIXlt converts the same instant to a list whose elements are the individual fields.

posix.R
stamp <- as.POSIXct("2026-11-19 14:30:00", tz = "UTC")
print(stamp)
print(class(stamp))
print(format(stamp, "%Y-%m-%d %H:%M:%S %Z"))
print(as.POSIXlt(stamp)$hour)

The timestamp is stored in UTC and printed with the UTC label. The as.POSIXlt conversion exposes the hour as the number 14. A POSIXct value is a single number, so it is compact and sorts correctly; POSIXlt is convenient for reading fields but should not be stored in a data frame column.

$ Rscript posix.R
[1] "2026-11-19 14:30:00 UTC"
[1] "POSIXct" "POSIXt" 
[1] "2026-11-19 14:30:00 UTC"
[1] 14

Parsing a timestamp

strptime parses text with an explicit format and returns a POSIXlt value. It is the reverse of format: every conversion specification in the pattern must match the input. A wrong pattern produces NA rather than an error, so the result should always be checked.

parsing.R
text <- "19 November 2026 14:30"
parsed <- strptime(text, format = "%d %B %Y %H:%M", tz = "UTC")
print(parsed)
print(class(parsed))
print(format(parsed, "%Y-%m-%dT%H:%M:%S"))

The text is read as the nineteenth of November 2026 at 14:30 UTC. The printed form and the class confirm that the result is a POSIX time. The final format call produces the ISO 8601 form with a T between the date and the time, which is the usual representation for exchange between systems.

$ Rscript parsing.R
[1] "2026-11-19 14:30:00 UTC"
[1] "POSIXlt" "POSIXt" 
[1] "2026-11-19T14:30:00"

A complete example

The next script schedules four tasks from a start date. Each duration is a number of days, and cumsum turns the durations into running totals, which are added to the start date. A sequence then counts the calendar days, and the weekday number removes Saturdays and Sundays.

project.R
start <- as.Date("2026-11-19")
tasks <- c(5, 10, 3, 7)
finish <- start + cumsum(tasks)
names(finish) <- c("design", "build", "test", "deploy")
print(finish)
days <- seq(start + 1, finish[4], by = "day")
working <- days[!format(days, "%u") %in% c("6", "7")]
cat("calendar days:", length(days), "\n")
cat("working days:", length(working), "\n")

Design ends on 24 November, build on 4 December, test on 7 December, and deploy on 14 December. The interval contains 25 calendar days and 17 working days, because four weekends fall inside it. The pattern %u numbers Monday as one and Sunday as seven, so removing six and seven leaves the working days.

$ Rscript project.R
      design        build         test       deploy 
"2026-11-24" "2026-12-04" "2026-12-07" "2026-12-14" 
calendar days: 25 
working days: 17 

Next steps

The strings article shows how to cut and join the text that dates are parsed from, and the regular expressions article validates date-like input before parsing. The lubridate article later in the series replaces the format codes with named functions and adds durations and periods.

Source

R documentation: as.Date

R documentation: strptime

R documentation: date-time classes

R documentation: difftime

In this article we have created dates with as.Date, formatted them with the conversion specifications, and read their parts with format, weekdays, months, and quarters. We added days, subtracted dates, and converted the difference to weeks. We created and parsed timestamps with POSIXct, POSIXlt, and strptime, and scheduled a small project while counting working days. The series continues with R subsetting and indexing.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all R Language tutorials.