It is important to understand the DateTime data type in python instead of creating our own data type for date and time. Python provides the DateTime data type by using the DataTime Package, which we will discuss in detail.
History of the DateTime Package in Python:-
DateTime Package in python was first released on Aug 5, 2008. it was the first 2.11.1 version. after that, it was continuously updated and upgraded by the team of the DateTime package and today we have the 4.3 version which is released on Oct 5, 2018. Currently, the project is maintained by a team of 4 individuals, Jens Vagelpohl, Hanno Schlichting, Michael Hwitz, and Tres Seaver.
How to Install the DateTime Package in Python?
To install the DateTime Package in python You can use the Pip and the following command.
pip install DateTime
How to return the list of Timezone available in Python?
Will this one is easy you just have to use the DateTime package which is available in Python. You have to import the Timezones function from the Datetime Package in Python by using the import statement and after then you have to call the Function just like Below.
from DateTime import Timezones timezones = set(Timezones()) print(timezones) for zone in timezones: print(zone)
This will print all the available timezone which are out here in the python DateTime package. the first five rows of Output of the above sample code are following.
How to print the Current Date and Time of a Zone in Python?
To print the current Date and time of a specific zone in Python we can use the DateTime Package in the following way. first, pass the timezone that you want the date and time e.g “us/Eastern” timezon second, you then get the timezone by calling the method timezone to call the method UTC DateTime() method to get the current date and time of the specified zone.The following code shows you the whole process.
from DateTime import DateTime e = DateTime('US/Eastern') e.timezone() print(e.utcdatetime())
The output of the above code will be the Date of the specified region that you want to print the date. In this case, it will output the “us/eastern” time and date.
How to convert Date into parts in Python?
To convert Date into parts in python we can use the DateTime Package in the following way. We will first convert the raw format of the date to the DateTime data type and then by calling the parts method of the object of the DateTime we can then print the parts of the date.
from DateTime import DateTime # passing the raw date to the DateTime z = DateTime('2014-03-24') # calling the parts method to convert to the parts parts=z.parts() # printing the parts of the date print(parts) # timezone of the date timezone=z.timezone() print(timezone)
The raw Date that we pass to the DateTime must have some constraints to follow. For example the year must be one digit , two-digit, or four-digit and no other digit is allowed in the date place. There are other constraints that are applied on the month and day as well. for more information about the constraint please have a look on the official documentation.
Conversion of the Date and Time in Python:-
Sometimes we need to convert one format date to another format date. DateTime Package in python an easy to use methods that can convert one format of the date to another format by just calling the methods a few examples are given below that can help you better understand the conversion.
what does the timetime() method do in DateTime Python?
The timetime method in the python DateTime package is a method that returns the floating-point value which defines the number of seconds since 1970 but it can reverse the process as well. we will see it in the code below.
from DateTime import DateTime dt = DateTime('US/Eastern') print(dt.timeTime())
the above sample code will output a floating-point value depending on the time that you run this code.
what does the isFuture method do in DateTime Python?
Suppose you want to know if the date is in the future or in past. in python, you can do it by using the future method of the DateTime package which will return the boolean value true if the date is in the future and the boolean value false if the date is not in the future. below is the sample code that will explain to you the function of the method.
from DateTime import DateTime dt = DateTime('Mar 9, 1997 13:45:00 US/Eastern') print(dt.isFuture()) dt1 = DateTime('Jan 1 3000') print(dt1.isFuture())
in the above code, it will print Flase for the first one and True for the second one as the first one if not in the future and the second date is actually in the future. similarly, there is another method that has very similar functionality that defines whether the date is in the past or not. below is the sample code that will show you if a date is in the past or not.
from DateTime import DateTime dt = DateTime('Mar 9, 1997 13:45:00 US/Eastern') print(dt.isPast()) dt1 = DateTime('Jan 1 3000') print(dt1.isPast())
List of the Frequent Used Methods in DateTime Package:-
Returns true if the year is the current year of the date.
Returns true if the month is the current month of the date.
this method will print the current day of the year.
this will print the current hour of the DateTime. but it will be in the context of the 24 hours.
this will print the current minute of the DateTime.
this will print the number of milliseconds since the epoch.
Returns the string for the DateTime Object.
this will show you the string with the date/time format in the international standard organization format.
Returns the current number of the week
Returns if one date is less than the passed dateObject or not.
Sample code for the above methods with output:
from DateTime import DateTime currentdate = DateTime('Aug 15, 2021 13:45:00 US/Eastern') past = DateTime("jan 13, 1993") # 1. return True is the date is not current year print(currentdate.isCurrentYear()) # output : True # 2. return True is the month is not in the current date print(currentdate.isCurrentMonth()) # output : True # 3. print the day of the date print(currentdate.day()) # output : 15 # 4. print the hour of the date print(currentdate.hour()) # output : 13 # 5. print the minutes of the date print(currentdate.minute()) # output : 45 # 6. print the no of milliseconds print(currentdate.millis()) # output : 1629049500000 # 7. print time in a precis way print(currentdate.PreciseTime()) # output : 13:45:00.000 # 8. print the time in iso format print(currentdate.ISO()) # output : 2021-08-15 13:45:00 # 9. print the number of the week print(currentdate.week()) # output : 32 # 10. compare the two dates print(currentdate.lessThanEqualTo(past)) # output : False
Summary and Conclusion:-
Date and time is an important Package in python which can make our work easy. Although we have discussed the most common methods that were most frequently used for further understanding the package you can have a look at the official documentation.
I am a software Engineer having 4+ Years of Experience in Building full-stack applications.