In this module, we will learn about how to solve the problems when it comes to a date in Python, covering the following topics:
Current Date and Time with Datetime in Python
Let us see how with the help of Python datetime module and a simple line of code we can get the current date in Python.
Python Datetime Example:
#import datetime python
a = datetime.datetime.now()
When the above code block is executed, it will display the current date (containing year, month, day, hour, minute, second, and microsecond in the same order). In the ‘datetime.datetime’ statement, the first datetime is the module and the second one is the datetime class inside of the datetime method which contains information from both date and time objects.
We can also display just one of the attributes of date in python, such as, just the year.
a = datetime.datetime.now()
Learn more about Python from this Python Training in New York to get ahead in your career!
Creating Date Objects in Python
In certain cases, we will want to create a custom date instead of just displaying the current date. In those cases, we can use the datetime() class constructor of the Python datetime module.
This Python datetime() class constructor requires three parameters to create a date, that is, year, month, and day.
a = datetime(2050,11,11)
The Python datetime() class constructor also takes parameters for time and timezone, which are hour, minute, second, microsecond, and tzone. But these parameters are optionally and rarely used. The default value of these parameters is set to 0.
The strftime() Method in Python Datetime
The datetime module in Python also has a method to convert the date objects into readable strings. So, we can display the month or day in a readable string format instead of numerical representation.
a = datetime.datetime(2020,6,19)
Now, we might be wondering, how do we specify that we want to display the month in a readable string format and what ‘%B’ and ‘%d’ are. Well, Python uses C style formatting to generate new strings. These are called format codes. There are different format codes for different attributes of the date object.
Following table contains a list of all legal format codes and their short descriptions:
||Short version of weekday
||Full version of weekday
||Weekday as a number starting from 0 to 6, where 0 is Sunday
||Day of the month in numerical format starting from 01 to 31
||Short version of month name
||Full version of month name
||Month as a number starting from 01 to 12
||Short version of a year, i.e., without indicating the century
||Year in full version
||Hour in 24-hour format starting from 00 to 23
||Hour in am-pm format; 00–12
||Minute starting from 00 to 59
||Second starting from 00 to 59
||Microsecond starting from 000000–999999
||Day of the year in numerical format, starting from 001 to 366
||Week of the year in numerical format, starting from 00 to 53, where Sunday is the first day of the week
||Week of the year in numerical format, starting from 00 to 53, where Monday is the first day of the week
||Local version of date and time
||Mon Mar 04 17:41:00 2018
||Local version of date
||Local version of time
Interested in learning Python? Check out the Python Training in Sydney!
We can use any of these codes with the strf method to display their respective values. The strftime method can take one or more than one format codes as argument, as shown in our example, where we have used ‘%B’ to display the full version of the month name in a readable string format and ‘%d’ to display the day of the month in a numerical format.
With this we come to the end of this module in Python Tutorial.Here we have learnt about dealing with date and time in Python with the help of datetime module. Now, if you want to know, how python can be used for data science, you can go through this blog on python for data science.
Further, check out our offers for Python training Course and also refer to the trending Python interview questions prepared by the industry experts.