Time and date
| Local time | |
| System time | |
| Format and parse | |
| Date arithmetic |
Local time
The calendar module answers dates and times as tuples. local_time gives a pair of date and time tuples for the machine's timezone.
universal_time answers the same in UTC, which is the safer default for servers.
1> calendar:local_time().
{{2026,10,10},{14,30,0}}
2> calendar:universal_time().
{{2026,10,10},{11,30,0}}
System time
The erlang:system_time function answers the system time in a unit you choose: seconds, milliseconds, or nanoseconds.
Use it for measuring elapsed time, and calendar for wall-clock dates.
1> erlang:system_time(second).
1791000000
2> erlang:monotonic_time(millisecond).
1234567
Format and parse
The calendar module converts between dates and day counts, which is the cheapest way to format or compare dates in plain Erlang.
For pretty output, print the tuple with tilde p and let the reader see the whole date.
1> io:format("~p~n", [calendar:local_time()]).
{{2026,10,10},{14,30,0}}
2> calendar:date_to_gregorian_days({2026,10,10}).
739751
Date arithmetic
Convert two dates to Gregorian day counts, subtract, and you have the days between them. The last_day_of_the_month function answers month lengths.
The calendar module also knows leap years, which the days arithmetic handles for free.
1> calendar:date_to_gregorian_days({2026,12,25}) - calendar:date_to_gregorian_days({2026,10,10}).
76
2> calendar:last_day_of_the_month(2026,2).
28
Next: JSON and term formats
Article author: Arthur Isaev