To start off with I think I should go over just a few very basic getting started examples of the datetime library. There are a number of ways to create an instance of the main datetime class in this library so maybe it is a good idea to start there. It might be called for to go over just a few simple basic examples of the other classes that there are to work with in the library, mainly the date, and timedelta classes.
The datetime might be the best class to go with in most cases. Or at least I am saying that because I am so accustomed to working with just one class that can be used for date and time rather than having more than one class to work with. However there are a few other classes, methods of those classes, as well as static methods of such classes that I think I should at least touch base on when it comes to a basic example section.
The now method of the datetime property of the datetime library will return an instance of the datetime class. However it will always return such a class instance for the current time. What if I want another such class instance that is at a point that in time that is before or after the current time? To create such a class instance I am going to want to use the datetime class directly.
The now method will just return an instance of datetime for the current time, to get a date object of a point in the past I will want to use the main date class to create such a date object.
So as I have covered briefly in the first section there is a timedelta class that is a special class that represents a time difference between two dates rather than a specific point in time. This timedelta class can be created by subtractive two date or datetime instances, it can also be used to create a new instance of these classed by adding the delta to one of them as a starting point. So in this section I am going to go over some simple quick examples of these datetime class instances.
In the basic section where I covered the time delta briefly one way to create an instance of a time delta is by subtractive two instances of a date. However I did not cover how to go about using an instance of this time delta class, so with that said one way to use a time delta is to just add or subtract a time delta to another distance of a date, or date time to get a new point in time in the form of a date or datetime class instance.
Although it is great that I can create timedelta instances by getting the difference between two dates, it would be nice to also be able to just create them directly. For this there is the timedelta class of the datetime module where I can just call the main constructor function and pass some arguments for days, or seconds. For example I can create a time delta class instance that is one billion seconds, and then I can add that difference to the date that i was born to get the date and time at which I turned on billion seconds old.
Now that I have some basic examples out of the way there is taking a closer look at the date class. Like many other classes there are many useful methods that can be called off of an instance of a date object.
There are properties for the year, month, and day of a date object.
The timetuple method of a date class instance can return a tuple of the properties of a date object. The tuple then can be looped over in a for loop.
I did not get around to writing about every little feature when it comes to the additional classes in the library that have to do with time zones, and the time of date alone. When I get around to editing this post at some point in the future there is of course expanding on thoses, but I think I would also like to create at least a few actually simple project examples also, and that might be a better move. When it comes to covering every little detail of where there is in terms of classes and functions there is always the official python docs after all.