Date and Time is one of the important topic in any programming language. In Python we can import the module named 'datetime' to work with Date and Time.
Let us start with the below example.
import datetime x = datetime.datetime.now() print(x)
Now, if we dissect the output,
So, with the below statement,
We get a 'datetime' object, 'x' that has Year, Month, Day, Hour, Minute, Second and Microsecond.
So, you got the date in clubbed format. Now, what if you want everything separately.
Well! Python provides that way as well.
import datetime x = datetime.datetime.now() year = x.year month = x.month day = x.day hour = x.hour minute = x.minute second = x.second microsecond = x.microsecond print("The full format is ", x) print("The year is ",year) print("The month is ",month) print("The day is ",day) print("The hour is ",hour) print("The minute is ",minute) print("The second is ",second) print("The microsecond is ",microsecond)
Now, if you look at the above output, we have separated the clubbed formatted date.
To display the year, you need to invoke 'x.year'.
Similar, logic applies for Month, Day, Hour, Minute, Second and Microsecond.
Now, let us see how to work with Date and Time separately.
To work only with Dates. We can only import 'date' class from the 'datetime' module.
Let us look at the below example.
from datetime import date today = date.today() print("Today's date is ",today)
So, in the above example, we have just imported the 'date' class from the 'datetime' module.
Next, we have invoked the Function 'today( )' of 'date' class to get today's date.
And if you see the output.
Only the date is displayed and not the time.
Next, let us see, how to work with time.
Similarly, to work only with Time. We can only import the 'time' module.
import time x = time.localtime() t = time.strftime("%H:%M:%S", x) print(t)
So, in the above example, we have used the 'localtime( )' Function from the 'time' module and got the unformatted time in the variable 'x'.
Then we have used 'strftime( )' Function to format the String.
- Where '%H' stands for Hour,
- '%M' stands for Minute,
- And '%S' stands for Second.
And we get the time in Hour, Minute and Seconds.
'strftime( )' Function itself needs an explanation. Let us look at it next.
To work with Date and Time and to format it properly. There is a Function called 'strftime( )'.
Apart from the 'time' module, it is also present in the 'datetime' module.
In 'datetime' module it takes just one argument and you can format the date and time in the way you want to.
Let us look at it in the below example.
import datetime x = datetime.datetime.now() t = x.strftime("%d-%B-%Y") print(t)
As we have seen earlier, with the below statement.
We get a 'datetime' object, 'x' that has Year, Month, Day, Hour, Minute, Second and Microsecond.
Then we have used the 'strftime( )' function to display the date in a proper format.
Where the formats,
- '%d' is to display the day.
- '%B' is to display the Month
- '%Y' is to display the Year.
And we get the date in the below format,
Now let us see all the formats available for 'strftime( )' function.
Directive | Meaning | Represented as |
---|---|---|
%y | Short Version for Year | 21 |
%Y | Full Version for Year | 2021 |
%H | Hour | 00-23 |
%I | Hour | 00-12 |
%p | AM/PM | AM/PM |
%M | Minute | 00-59 |
%j | Day number in a year | 001-366 |
%U | Week number in a year. Sunday is the first day of week. | 00-53 |
%W | Week number of year. Monday is the first day of week. | 00-53 |
%c | Date and Time | Sun Jan 10 20:38:00 2021 |
%x | Date | 01/10/21 |
%X | Time | 20:38:00 |
%S | Second | 00-59 |
%f | Microsecond | 000000-999999 |
%z | UTC offset | +0100 |
%a | Short form of Weekday | Sun |
%A | Full form of Weekday | Sunday |
%w | Weekday in number | 0-6, 0 is Sunday |
%d | Day of month | 01-31 |
%b | Name of the Month in Short | Jan |
%B | Name of the Month in Full | January |
%m | Month in the form of number | 01-12 |
%Z | Timezone | CST |