Julia Dates Tutorial
last modified November 18, 2026
Julia's Dates standard library provides types for working with
calendar dates and clock times. It can parse text into dates, format dates as
text, read individual components such as the year or weekday, perform
arithmetic, and generate sequences of dates. None of this requires an
external package.
This tutorial introduces Date, Time and
DateTime, the dateformat macro, parsing and
formatting, periods, arithmetic, and ranges. Fixed dates are used throughout
so the output is reproducible; all examples were verified with Julia 1.13.4.
Date, Time and DateTime
The library defines three main types. A Date stores only a
calendar day, a Time stores only a time of day, and a
DateTime stores both. Their constructors take numeric components
in the order year, month, day, hour, minute, second.
using Dates println(Date(2026, 11, 18)) println(Time(9, 30, 15)) println(DateTime(2026, 11, 18, 9, 30)) println(typeof(Date(2026, 11, 18)))
Dates print in the ISO 8601 style yyyy-mm-dd, which sorts
correctly as text. Times print as hh:mm:ss. A
DateTime separates the date and time with the letter
T; the seconds are shown even though they were not supplied,
because they default to zero. The using Dates line makes the
library's names available.
$ julia main.jl 2026-11-18 09:30:15 2026-11-18T09:30:00 Date
Parsing with dateformat
Parsing means converting text into a date object. The
dateformat"..." macro describes the layout of the text using
tokens such as yyyy for a four-digit year, mm for a
two-digit month, and dd for a two-digit day. Passing the layout
to Date or DateTime performs the conversion.
using Dates
println(Date("2026-11-18", dateformat"yyyy-mm-dd"))
println(DateTime("2026-11-18 09:30", dateformat"yyyy-mm-dd HH:MM"))
println(tryparse(Date, "18/11/2026", dateformat"dd/mm/yyyy"))
println(tryparse(Date, "not a date", dateformat"yyyy-mm-dd"))
The first two calls succeed and print dates in the standard display format.
The HH and MM tokens read a two-digit hour and
minute. While Date(...) throws an error on invalid input,
tryparse returns nothing instead, which is useful
when processing data that may be malformed. The last line demonstrates that
behaviour.
$ julia main.jl 2026-11-18 2026-11-18T09:30:00 2026-11-18 nothing
Formatting Dates
The Dates.format function converts a date object into text. Its
second argument can be an ordinary format string or a
dateformat macro; the tokens are the same ones used for parsing.
using Dates d = Date(2026, 11, 18) println(Dates.format(d, "dd/mm/yyyy")) println(Dates.format(d, dateformat"yyyy-mm-dd")) println(Dates.format(DateTime(2026, 11, 18, 9, 5), "HH:MM")) println(Dates.format(d, "yyyy-mm-dd E"))
The first line changes the order and the separators. The second reproduces
the ISO layout. The third prints only the time part, and the fourth uses the
token E to add the weekday name. Formatting is the inverse of
parsing: the same date can be written in many textual forms while remaining
one underlying value.
$ julia main.jl 18/11/2026 2026-11-18 09:05 2026-11-18 Wednesday
Reading Date Components
Functions such as year, month and day
extract parts of a date. dayofweek numbers the weekday with
Monday as one, while dayname and monthname return
their names. The related functions isleapyear and
daysinmonth answer calendar questions.
using Dates
d = Date(2026, 11, 18)
println(year(d), " ", month(d), " ", day(d))
println(hour(DateTime(2026,11,18,9,30,15)), ":",
minute(DateTime(2026,11,18,9,30,15)), ":",
second(DateTime(2026,11,18,9,30,15)))
println(dayofweek(d), " ", dayname(d), " ", monthname(d))
println(isleapyear(2024), " ", isleapyear(2026), " ", daysinmonth(2024, 2))
The date is the eighteenth day of the eleventh month of 2026. Its time part,
taken from a separate DateTime, is 9:30:15.
November 18, 2026 is a Wednesday, the third day of the week. The year 2024 is
a leap year, so February has 29 days; 2026 is not.
$ julia main.jl 2026 11 18 9:30:15 3 Wednesday November true false 29
Date Arithmetic
Adding a period to a date produces another date; subtracting two dates produces the period between them. Months and years are calendar units, so adding them keeps the day of the month when possible.
using Dates d = Date(2026, 1, 1) println(d + Day(10)) println(d - Day(5)) println(d + Month(2)) println(Date(2026, 12, 31) - Date(2026, 1, 1)) println(typeof(Date(2026, 12, 31) - Date(2026, 1, 1)))
Ten days after New Year's Day is January 11, while five days before it falls
in the previous year. Adding two months moves to March 1. Subtracting two
dates yields a Day value, displayed as 364 days.
Subtracting two DateTime values instead gives a
Millisecond, the library's finest built-in unit.
$ julia main.jl 2026-01-11 2025-12-27 2026-03-01 364 days Day
Period Types
A period is a length of time rather than a point in time. The main
types are Year, Month, Week,
Day, Hour, Minute, Second
and Millisecond. The Dates.value function returns
the underlying number, and periods of different units can be added.
using Dates println(typeof(Day(3)), " ", typeof(Month(3)), " ", typeof(Year(3))) println(Dates.value(Day(3))) println(Day(1) + Hour(12)) println(Date(2026, 11, 18) + Week(1))
Each constructor creates a value of its own type. A single period holds one
number, which Dates.value exposes. Adding days and hours yields
a CompoundPeriod printed as 1 day, 12 hours, and a
week added to a date moves it forward seven days.
$ julia main.jl Day Month Year 3 1 day, 12 hours 2026-11-25
Date Ranges
Dates can be used in ranges just like numbers. The range syntax
start:step:stop produces a lazy sequence; collect
turns it into an array of dates.
using Dates println(collect(Date(2026,1,1):Day(1):Date(2026,1,5))) println(collect(Date(2026,1,1):Month(1):Date(2026,4,1)))
The first range lists five consecutive days. The second uses
Month(1) as its step and lists the first day of each month from
January through April. Ranges print their elements using the constructor
form, for example Date("2026-01-01").
$ julia main.jl
[Date("2026-01-01"), Date("2026-01-02"), Date("2026-01-03"), Date("2026-01-04"), Date("2026-01-05")]
[Date("2026-01-01"), Date("2026-02-01"), Date("2026-03-01"), Date("2026-04-01")]
Comparing Dates
Date objects support the usual comparison operators, so they can be sorted or tested against a deadline. Subtracting a later date from an earlier one gives a negative period.
using Dates a = Date(2026, 11, 18) b = Date(2026, 12, 1) println(a < b) println(a == b) println(b - a) println(Dates.value(b - a))
The first date is earlier than the second, so a < b is
true, and they are not equal. The difference is 13 days;
Dates.value extracts that count as a plain integer, which is
handy for arithmetic and display.
$ julia main.jl true false 13 days 13
A Practical Example
As a small real-world task, we compute the number of days between a fixed starting date and a deadline, then print the deadline in a chosen format. Using explicit dates keeps this example deterministic.
using Dates
deadline = Date(2026, 12, 25)
today_fixed = Date(2026, 11, 18)
remaining = deadline - today_fixed
println("Days left: ", Dates.value(remaining))
println("Formatted: ", Dates.format(deadline, dateformat"yyyy-mm-dd"))
Subtracting the two dates returns a Day period whose value is
37. The second line formats the deadline back to text. Combining parsing,
comparison, arithmetic and formatting in this way covers most everyday date
work.
$ julia main.jl Days left: 37 Formatted: 2026-12-25
Source
In this article we have worked with dates in Julia: the Date,
Time and DateTime types, parsing with the
dateformat macro, formatting, date components, periods,
arithmetic, comparisons, and ranges. More on text output is in
Julia string functions, and ranges fit
with the material in Julia iterators.
Author
List all Julia tutorials.