In Python, there is a module which is comprised of various functions to deal, manipulate and work with dates and date objects. This module is known as Python datetime module. We can simply start working with dates by importing this module and using the functions and classes it provides.
In this module we will learn about working with dates in Python and cover the following topics.
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.
You can also display just one of the attributes of date, such as, just the year. For example:
In certain cases, you will want to create a custom date instead of just displaying the current date. In those cases, you can use datetime() class constructor of the Python datetime module.
This datetime() class constructor requires three parameters to create a date, that is, year, month and day.
The datetime() class constructor also takes parameters for time and timezone, which are hour, minute, second, microsecond, tzone. But these parameters are optionally and rarely used. The default value of these parameters is set to 0.
In Python datetime module also has a method to convert the date objects into readable strings. So, you can display the month or day in readable string format instead of numerical representation.
Now you might be wondering, how did we specify that we want to display month in readable string format and what is “%B” and”%d”. Well, Python uses C style formatting to generate new strings. These are called format codes. There are different format codes for different attributes of date object.
Following table contains a list of all legal format codes and their short descriptions.
|%a||Short version of weekday||Wed|
|%A||Full version of weekday||Wednesday|
|%w||Weekday as a number, starting from 0 to 6 where 0 is Sunday||3|
|%d||Day of month in numerical format starting from 01 to 31||15|
|%b||Short version of month name||Dec|
|%B||full version of month name||December|
|%m||Month as a number starting from 01 to 12||12|
|%y||Short version of year, that is, without century||19|
|%Y||Year, full version||2019|
|%H||Hour in 24 hour format starting from 00-23||17|
|%I||Hour in am-pm format, 00-12||05|
|%M||Minute starting from 00 to 59||41|
|%S||Second starting from 00 to 59||08|
|%j||Day number of year, starting from 001 to 366||365|
|%U||Week number of year, Sunday being the first day of week, starting from 00 to 53||52|
|%W||Week number of year, Monday being the first day of week, 00-53||52|
|%c||Local version of date and time||Mon Mar 04 17:41:00 2018|
|%x||Local version of date||03/04/19|
|%X||Local version of time||19:21:00|
You can use any of these codes with strf method to display their respective values. Strftime method can take one or more than one format codes as argument. As shown in the above example, we have used %B to display the full version of the month name in readable string format and %d to display the day of the month in numerical format.Previous Next
Download Interview Questions asked by top MNCs in 2019?