TExcelTAD
Home › Julia › Julia Dates

Julia Dates

The Dates standard library handles calendar dates, times of day, durations and formatting. Its two main types are Date (a day, no time) and DateTime (a day plus a time with millisecond precision).

Note: today() and now() return the current date and time. The examples here use fixed dates so their output never changes.

julia · try it yourself
using Dates

d = Date(2025, 7, 14)
dt = DateTime(2025, 7, 14, 9, 30, 15)
println(d)
println(dt)
println(year(d), " ", month(d), " ", day(d))
println(dayname(d), ", ", monthname(d))
println(dayofweek(d), " ", dayofyear(d), " ", isleapyear(d))
println(hour(dt), ":", minute(dt))
2025-07-14
2025-07-14T09:30:15
2025 7 14
Monday, July
1 195 false
9:30

Periods and date arithmetic

Periods such as Day(3), Week(2), Month(1), Year(1), Hour(5) can be added to or subtracted from dates. Subtracting two dates gives a period:

julia · try it yourself
using Dates

start = Date(2025, 1, 31)
println(start + Day(10))
println(start + Month(1))         # clamps to the last valid day
println(start - Week(2))
println(Date(2025, 12, 25) - Date(2025, 7, 14))
println(Dates.value(Date(2026, 1, 1) - Date(2025, 1, 1)), " days in 2025")

meeting = DateTime(2025, 3, 9, 14, 0)
println(meeting + Hour(26) + Minute(45))
println(Dates.canonicalize(Minute(1501)))
2025-02-10
2025-02-28
2025-01-17
164 days
365 days in 2025
2025-03-10T16:45:00
1 day, 1 hour, 1 minute

Calendar arithmetic follows common sense: adding one month to 31 January gives the last day of February.

Ranges of dates

Dates work with ranges, so you can loop over days, weeks or months:

julia · try it yourself
using Dates

for d in Date(2025, 3, 1):Week(1):Date(2025, 3, 31)
    println(d, " ", dayabbr(d))
end
first_mondays = [tonext(d -> dayofweek(d) == 1, Date(2025, m, 1) - Day(1)) for m in 1:3]
println(first_mondays)
2025-03-01 Sat
2025-03-08 Sat
2025-03-15 Sat
2025-03-22 Sat
2025-03-29 Sat
[Date("2025-01-06"), Date("2025-02-03"), Date("2025-03-03")]

Adjusters like tonext, toprev, firstdayofmonth and lastdayofmonth move a date to the next matching day or to month boundaries.

Formatting and parsing

Dates.format turns a date into text using a format string, and the Date/DateTime constructors parse text with the same codes:

Code Meaning Example
yyyy 4-digit year 2025
mm / m month number (padded / not) 07 / 7
u / U month name short / full Jul / July
dd / d day of month 04 / 4
e / E weekday short / full Fri / Friday
HH, MM, SS hour, minute, second 09, 05, 00
julia · try it yourself
using Dates

d = Date(2025, 7, 4)
println(Dates.format(d, "dd/mm/yyyy"))
println(Dates.format(d, "E, U d, yyyy"))
println(Dates.format(DateTime(2025, 7, 4, 9, 5), "yyyy-mm-dd HH:MM"))

parsed = Date("14.02.2026", dateformat"dd.mm.yyyy")
println(parsed, " is a ", dayname(parsed))
println(DateTime("2025-11-30T18:45:00"))
println(tryparse(Date, "2025-02-30") === nothing)
04/07/2025
Friday, July 4, 2025
2025-07-04 09:05
2026-02-14 is a Saturday
2025-11-30T18:45:00
true

dateformat"..." creates the format once at parse time, which is faster when parsing many strings.

Practical example: age and countdown

julia · try it yourself
using Dates

function age_on(birth::Date, on::Date)
    years = year(on) - year(birth)
    # subtract one if the birthday hasn't happened yet this year
    if (month(on), day(on)) < (month(birth), day(birth))
        years -= 1
    end
    return years
end

reference = Date(2025, 10, 1)
println(age_on(Date(1990, 12, 5), reference))
println(age_on(Date(1990, 3, 5), reference))

deadline = Date(2025, 12, 31)
days_left = Dates.value(deadline - reference)
workdays = count(d -> dayofweek(d) <= 5, reference:Day(1):deadline)
println(days_left, " days left, ", workdays, " of them weekdays")
34
35
91 days left, 66 of them weekdays

Test yourself

What is Date(2025, 1, 31) + Month(1)?

  1. 2025-03-03
  2. 2025-02-28
  3. 2025-02-31
  4. an error

What type do you get from subtracting two Dates?

  1. Int64
  2. A Day period
  3. A Date
  4. A Float64 number of years

Which format string produces 03/15/2025 for 15 March 2025?

  1. dd/mm/yyyy
  2. mm/dd/yyyy
  3. yyyy/mm/dd
  4. m/d/yy