Julia Dates and Time
Julia provides the following three types for handling dates and times through the standard library Dates module:
- Date: represents a date, precise to the day, displaying only the date.
- DateTime: represents a date and time, precise to the millisecond.
- Time: represents a time of day, precise to the nanosecond, representing a specific moment within the 24 hours of a day.
Before use, we need to import the Dates module first:
using Dates
The Date and DateTime types can be parsed from integers or Period types.
Period is based on date values and represents spans of time such as years, months, and days:
Year # 年
Quarter # 季度
Month # 月
Week # 周
Day # 日
Hour # 小时
Minute # 分钟
Second # 秒
Millisecond # 毫秒
Microsecond # 微秒(仅 Time)
Nanosecond # 纳秒(仅 Time)
Both Date and DateTime are subtypes of the abstract type TimeType.
The following figure shows the relationship among date types; click the image to enlarge it:
Get Current Date and Time
Example
julia
using
Dates
Current date and time
julia
rightnow = now
(
)
2022
-05-11T08:
41
:
15.917
Current date
julia
today_date = today
(
)
2022
-05-
11
Current time in the UTC time zone
julia
now
(
UTC
)
2022
-05-11T00:
44
:
20.136
The current moment (time only, without a date)
julia
Dates.
Time
(
now
(
)
)
08:
41
:
15.917
Create an object with a specified date
julia
theday = Date
(
2022
,
5
,
6
)
2022
-05-06
Parsing and formatting
You can parse date text with a format string, and also format a date object into a string of any form:
Example
Parse by format
julia
Dates.
DateTime
(
"20220429 120000"
,
"yyyymmdd HHMMSS"
)
2022
-04-29T12:00:00
julia
Dates.
DateTime
(
"19/04/2022 17:42"
,
"dd/mm/yyyy HH:MM"
)
2022
-04-19T17:
42
:00
Output by format
julia
Dates.
format
(
now
(
)
,
"yyyy-mm-dd HH:MM:SS"
)
"2022-05-11 09:01:07"
Built-in formats
julia
Dates.
format
(
now
(
)
, Dates.
RFC1123Format
)
"Wed, 11 May 2022 09:15:00"
The following table gives the date format codes:
| Character | Date/Time element |
|---|---|
| y | Represents the year, for example: yyyy => 1984, yy => 84 |
| m | Represents the month, for example: m => 7 or 07 |
| u | Represents the abbreviated month name, for example: Jun |
| U | Represents the full month name, for example: January |
| e | Represents the abbreviated weekday name, for example: Mon |
| E | Represents the full weekday name, for example: Monday |
| d | Represents the day, for example: 1 or 01 |
| H | Represents the hour, for example: HH => 00 |
| M | Represents the minute, for example: MM => 00 |
| S | Represents the second, for example: S => 00 |
| s | Represents the millisecond, for example: .000 |
Example
julia
Dates.
Date
(
"Sun, 27 Sep 2022"
,
"e, d u y"
)
2022
-09-
27
julia
Dates.
DateTime
(
"Sun, 27 Sep 2022 10:25:10"
,
"e, d u y H:M:S"
)
2022
-09-27T10:
25
:
10
Extract date components
After creating a date-time object, you can use the corresponding functions to get components such as year, month, day, hour, minute, and second:
Example
julia
theday = Date
(
2022
,
5
,
6
)
Create a date object
2022
-05-06
julia
year
(
theday
)
2022
julia
month
(
theday
)
5
julia
day
(
theday
)
6
Hour, minute, and second of the current time
julia
rightnow = now
(
)
2022
-05-11T08:
51
:
45.342
julia
minute
(
rightnow
)
51
julia
hour
(
rightnow
)
8
julia
second
(
rightnow
)
45
Day of the week (1=Monday ... 7=Sunday)
julia
dayofweek
(
theday
)
5
English name of the day of the week
julia
dayname
(
theday
)
"Friday"
(year, month, day) tuple
julia
yearmonthday
(
theday
)
(
2022
,
5
,
6
)
Which occurrence of that weekday within this month
julia
dayofweekofmonth
(
theday
)
1
Date arithmetic
We can perform arithmetic operations on date objects. For example, we compute how many days two dates differ by:
Example
julia
day1 = Date
(
2022
,
1
,
17
)
2022
-01-
17
julia
day2 = Date
(
2022
,
3
,
23
)
2022
-03-
23
julia
day2 - day1
65
days
Display in different units
julia
Dates.
canonicalize
(
Dates.
CompoundPeriod
(
day2 - day1
)
)
9
weeks,
2
days
We can also add to dates, for example computing the date 2 years and 6 months later:
Example
julia
rightnow = now
(
)
2022
-05-11T09:01:
07.946
julia
rightnow + Dates.
Year
(
20
)
- Dates.
Month
(
6
)
2042
11
-11T09:01:
07.946
The date 6 days later
julia
rightnow + Dates.
Day
(
6
)
2022
-05-17T09:01:
07.946
Date ranges
Julia can create dates for a specified interval through an iterable range (interval range) object. In the example given below, we create an iterator that generates the first day of each month:
Example
julia
date_range = Date
(
2011
,
1
,
1
)
:Dates.
Month
(
1
)
:Date
(
2022
,
1
,
1
)
Dates.
Date
(
"2011-01-01"
)
:Dates.
Month
(
1
)
:Dates.
Date
(
"2022-01-01"
)
In the above range object, we can use filter() to find the dates that fall on weekdays:
Example
julia
weekdaysfromrange =
filter
(
dy -
dayname
(
dy
)
!
=
"Saturday"
&&
dayname
(
dy
)
!
=
"Sunday"
, date_range
)
94
-element
Vector
{
Dates.
Date
}
:
2011
-02-01
2011
-03-01
2011
-04-01
...
Recurring dates
We can look up recurring dates within a range, such as every Sunday:
Example
julia
filter
(
d -
dayname
(
d
)
==
"Sunday"
, date_range
)
20
-element
Vector
{
Dates.
Date
}
:
2011
-05-01
2012
-01-01
...
Rounding dates and times
The round(), floor(), and ceil() functions can also be used to round dates, so as to adjust dates forward or backward:
Example
julia
now
(
)
2022
-05-11T09:
17
:
49.824
Round to the nearest 15 minutes
julia
Dates.
format
(
round
(
DateTime
(
now
(
)
)
, Dates.
Minute
(
15
)
)
, Dates.
RFC1123Format
)
"Wed, 11 May 2022 09:15:00"
The ceil() function adjusts the date/time forward:
Example
julia
theday = Date
(
2022
,
5
,
6
)
2022
-05-06
Next month
julia
ceil
(
theday, Dates.
Month
)
2022
-06-01
Next year
julia
ceil
(
theday, Dates.
Year
)
2023
-01-01
Next week
julia
ceil
(
theday, Dates.
Week
)
2022
-05-09
Unix time
UNIX time, also called POSIX time, is the time representation used by UNIX or UNIX-like systems: the total number of seconds from UTC 1970-01-01 00:00:00 up to now, ignoring leap seconds.
The time() function returns the Unix time value:
Example
julia
time
(
)
1.652232489777e9
The unix2datetime() function converts Unix time into a date/time object:
Example
julia
Dates.
unix2datetime
(
time
(
)
)
2022
-05-11T01:
28
:
23.493
Internal value
DateTimes are stored in units of milliseconds; we can use the Dates.value function to get the internal numeric value in milliseconds:
Example
julia
moment = now
(
)
2022
-05-11T09:
31
:
31.037
julia
Dates.
value
(
moment
)
63787944691037
julia
moment.
instant
Dates.
UTInstant
{
Millisecond
}
(
Millisecond
(
63787944691037
)
)
Timing
Julia provides the @elapsed macro, which returns the time (in seconds) required to execute an expression:
Example
julia
function
foo
(
n
)
for
i
in
1
:n
x = sin
(
rand
(
)
)
end
end
foo
(
generic
function
with
1
method
)
julia
@elapsed foo
(
100000000
)
1.360567967
julia
@
time
foo
(
100000000
)
1.363258
seconds
More examples of constructing dates
Example
julia
DateTime
(
2013
)
2013
-01-01T00:00:00
julia
DateTime
(
2013
,
7
)
2013
-07-01T00:00:00
julia
DateTime
(
2013
,
7
,
1
,
12
,
30
,
59
)
2013
-07-01T12:
30
:
59
julia
DateTime
(
2013
,
7
,
1
,
12
,
30
,
59
,
1
)
2013
-07-01T12:
30
:
59.001
julia
Date
(
2013
,
7
,
1
)
2013
-07-01
Construct with Period types
julia
Date
(
Dates.
Year
(
2013
)
, Dates.
Month
(
7
)
, Dates.
Day
(
1
)
)
2013
-07-01
