YouTip LogoYouTip

Julia Date Time

Julia provides the following three functions to handle dates and times through the Dates module: * **Date**: represents a date, accurate to the day, displays only the date. * **DateTime**: represents date and time, accurate to milliseconds. * **DateTime**: represents date and time, accurate to nanoseconds, representing a specific moment in 24 hours of a day. Before using, we need to import the Dates module first: ```julia import Dates Date and DateTime types can be parsed from integers or Period types. Period is based on date values, representing years, months, days, etc.: ```julia Period Year Quarter Month Week Day Hour Minute Second Millisecond Microsecond Nanosecond Date and DateTime are both subtypes of the abstract type TimeType. The following image shows the relationship between date types, click to enlarge: [!(#)](#) Output the time of the date: ## Example ```julia julia> import Dates julia> rightnow = Dates.Time(Dates.now())# time 08:41:15.917 julia> theday = Dates.Date(2022,5,6)# date 2022-05-06 julia> today_date = Dates.today() 2022-05-11 julia> Dates.now(Dates.UTC) 2022-05-11T00:44:20.136 # Formatting Time ```julia 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 The following table gives date format codes, through which we can format our dates: | Character | Date/Time Element | | --- | --- | | Y | Represents year, for example: yyyy => 1984, yy => 84 | | m | Represents year, for example: m => 7 or 07 | | u | Represents abbreviated month name, for example: Jun | | U | Represents full month name, for example: January | | e | Represents abbreviated day of week, for example: Mon | | E | Represents full day of week, for example: Monday | | d | Represents day, for example: 1 or 01 | | H | Represents hour, for example: HH => 00 | | M | Represents minute, for example: MM => 00 | | S | Represents second, for example: S => 00 | | s | Represents millisecond, for example: .000 | ## Example ```julia 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 Through the examples above, we have learned about some date and time objects. Next, we can use these objects to get data (including year, month, day, minute, second, hour, etc.): ## Example ```julia julia> theday = Dates.Date(2022,5,6)# create date object 2022-05-06 # Next, get the data from theday julia> Dates.year(theday) 2022 julia> Dates.month(theday) 5 # Get current time data julia> rightnow = Dates.now() 2022-05-11T08:51:45.342 julia> Dates.minute(rightnow) 51 julia> Dates.hour(rightnow) 8 julia> Dates.second(rightnow) 45 # Get month and day of week julia> Dates.dayofweek(theday) 5 julia> Dates.dayname(theday) "Friday" julia> Dates.yearmonthday(theday) (2022, 5, 6) julia> Dates.dayofweekofmonth(theday) 1 ### Date Operations We can perform arithmetic operations on date objects. For example, we calculate how many days difference between two dates: ## Example ```julia julia> day1 = Dates.Date(2022,1,17) 2022-01-17 julia> day2 = Dates.Date(2022,3,23) 2022-03-23 julia> day2 - day1 65 days # Using different time units julia> Dates.canonicalize(Dates.CompoundPeriod(day2 - day1)) 9 weeks, 2 days We can also add to dates, for example, calculating the date after 2 years and 6 months: ## Example ```julia julia> rightnow = Dates.now() 2022-05-11T09:01:07.946 julia> rightnow + Dates.Year(20) + Dates.Month(6) 2042-11-11T09:01:07.946 # Date after 6 days julia> rightnow + Dates.Day(6) 2022-05-17T09:01:07.946 ### Date Range Julia can create dates for a specified range through iterable range objects. In the example below, we will create an iterator that generates the first day of each month. ## Example ```julia date_range = Dates.Date(2011,1,1):Dates.Month(1):Dates.Date(2022,1,1) Dates.Date("2011-01-01"):Dates.Month(1):Dates.Date("2022-01-01") In the above range object, we can find which ones are weekdays. Here we need to create an anonymous function for filter(), which will determine whether a given date is a weekday: ## Example ```julia julia> weekdaysfromrange = filter(dy -> Dates.dayname(dy)!= "Saturday"&& Dates.dayname(dy)!= "Sunday" , date_range) 94-element Vector{Dates.Date}: 2011-02-01 2011-03-01 2011-04-01 2011-06-01 2011-07-01 2011-08-01 2011-09-01 2011-11-01 2011-12-01 2012-02-01 โ‹ฎ 2021-02-01 2021-03-01 2021-04-01 2021-06-01 2021-07-01 2021-09-01 2021-10-01 2021-11-01 2021-12-01 ### Rounding Dates and Time We often use round(),
โ† Git QuizMime Types โ†’