Julia> daystoxmas = Dates.Date(Dates.year(Dates.now()), 12, 25) - Dates.today() Julia> Dates.now() + Dates.Week(2) + Dates.Hour(12) Here's the date and time for two weeks and 12 hours from now: Julia> Dates.now() - Dates.Year(2) - Dates.Month(6)Īnd similarly for weeks and hours. Julia> birthday + Dates.Year(20) + Dates.Month(6) So, to add 20 years and 6 months to the birthday date: ![]() For example, Dates.Year(20) defines a period of 20 years, and Dates.Month(6) defines a period of 6 months. ![]() constructor functions to specify the period. To add and subtract periods of time to date and date/time objects, use the Dates. Julia> convert(Dates.Millisecond, Dates.Period(Dates.today() - Dates.Date(2016, 1, 1))) Julia> convert(Dates.Day, Dates.Period(Dates.today() - Dates.Date(2016, 1, 1))) Julia> Dates.canonicalize(Dates.CompoundPeriod(datetimenow - armistice))ĥ138 weeks, 5 days, 5 hours, 46 minutes, 1 second, 541 milliseconds Julia> Dates.Period(datetoday - birthday) Which you can convert to Dates.Days or Dates.Milliseconds or some other unit: Subtracting two dates or datetimes to find the difference is the most obvious one: You can do arithmetic on dates and date/time objects. You can also find days relative to a date, such as the first day of the week containing that day, using the adjusting functions, described below. The last function, dayofweekofmonth(birthday) (day of week of month), tells us that the 15th of March, 1997, was the third Saturday of the month. Two of those functions are very similarly named: the Dates.daysofweekinmonth() (days of week in month) function tells you how many days there are in the month with the same day name as the specified day - there are five Tuesdays in the current month (at the time of writing). Julia> Dates.daysofweekinmonth(datetoday) There's also a bunch of other useful ones: For both date and datetime objects, you can obtain the year, month, day, and so on: Once you have a date/time or date object, you can extract information from it with the following functions. See Date Formatting below for more examples.ĭate and time queries Julia> vacation = Dates.DateTime("T08:20:07") # defaults to expecting ISO8601 format To create an object from a formatted string, use the DateTime() function in Dates, and supply a suitable format string that matches the formatting: Sometimes you want UTC (the reference time for the world, without local adjustments for daylight savings): (We used Dates.now() earlier to define rightnow, then converted it to a Dates.Time using Dates.Time().) The Dates.now() function returns a DateTime object for the current instant in time: The Dates.today() function returns a Date object for the current date: Julia> birthday = Dates.Date(1997,3,15) # a Dates.Date object Julia> rightnow = Dates.Time(Dates.now()) # a Dates.Time object Use one of these constructors to make the type of object you want: A Dates.DateTime object is a combination of a date and a time of day, and so it specifies an exact moment in time.A Dates.Date object represents just a date: no time zones, no daylight saving issues, etc.It doesn't say anything about the day of the week, or the year, though. ![]() A Dates.Time object represents a precise moment of time in a day.There are three main datatypes available: Shows the hierarchy of date and date-time types in Julia Date, Time, and DateTimes ![]() This diagram shows the relationship between the various types used to store Times, Dates, and DateTimes. However, if you add the line using Dates to your code, this brings all exported Dates functions into Main, and they can be used without the Dates. Dates.dayofweek(dt), as shown in this chapter. If you use import Dates functions, you’ll need to prefix every function with an explicit Dates., e.g. To use any of the time and date functions, you must do one of the following: Working with dates and times įunctions for working with dates and times are provided in the standard package Dates.
0 Comments
Leave a Reply. |