We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Working with Date and Time in Julia
Julia provides a library to use Dates for dealing with date and time. The Dates module comes inbuilt with the Julia we just need to import it in order to use it. Now we need to prefix every function with an explicit type Dates, e.g Dates.Date. If you don’t want to prefix on each function just add using Dates in your code and then you will be able to use any function of Dates without using Dates as a prefix.
The Dates module provides supplies classes to work with date and time. There are many functions available that are used to manipulate Date and Time in Julia.
For working with Date the DateTime provides two main Modules: Date and DateTime are mostly used. They both are subtypes of the abstract TimeType.
Getting Different Types of Dates
Dates.Date( year, month, date): It returns date with provided parameters. It is independent of time zones.
Dates.Date(year): It returns date with the provided year, and the month, the day is set to 01
Dates.Date(year, month): It returns date with the provided year and month, and the date is set to 01.
Dates.DateTime( year, month, date, hour, min, sec): It returns date and time and it specifies the exact moment in time. The accuracy is to a millisecond.
Dates.today(): It provides the Date object for the current date
Dates.now(): It returns a DateTime object for the current instance in time.
Dates.now(Dates.UTC): It returns date with Coordinated Universal Time (UTC) time zone.
In order to format Dates, we need to refer the characters which represent/refer date elements. Here is a table for referring to what each character represents.
Code
Reference
Format
Example
y
year digit
yyyy or yy
2013
m
month digit
m
5 or 05
u
month name
u
Feb
U
month full name
U
February
e
day of week
e
Mon
E
day of the week full
E
Monday
d
day in number
d
1
H
hour digit
HH
11
M
minute digit
MM
22
S
second digit
S
00
s
millisecond digit
s
.001
We can these formatting strings with functions such as DateTime(), Dates.format() and Date() The following examples will show various date formatting methods:
Also, we can convert a date string from one format to other by using DateTime() to convert into DateTime object and then use DateFormat() to display/output to a different format.
For obtaining time in Julia we use Dates.Time() functionmostly. which follows the proleptic Gregorian calendar. The different types of time are as follows:
Dates.Time(Dates.now()) it returns a time in HH:MM:SS:ss and it takes Date object.
Dates.minute( t) it gives the minute of the Dates.Time object passed.
Dates.hour(t) it gives the hour of the Dates.Time object passed.
Dates.second(t) it gives the second of the Dates.Time object passed.
It is also referred to as UNIX time. It is used to deal with timekeeping. It is a count of the number of seconds that have elapsed since the beginning of the year 1970.
The time() function, when used without any arguments, returns the Unix/Epoch time value of the current second.