Python DateTime, TimeDelta, Strftime(Format) with Examples

In Python, date, time and datetime classes provides a number of function to deal with dates, times and time intervals. Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps. Whenever you manipulate dates or time, you need to import datetime function.

The datetime classes in Python are categorized into main 5 classes.

How to Format Date and Time Output with Strftime()

As of now we have learned, how to use datetime and date object in Python. We will advance a step further and learn how to use a formatting function to format Time and Date.

Step 1) First we will see a simple step of how to format the year. It is better to understand with an example.

Just by defining control code like %I/H for hour, % M for minute, %S for second, one can call time for different formats

12 hours time is declared [print now.strftime(“%I:%M:%S %P) ]

24 hours time is declared [print now.strftime(“%H:%M”)]

Here is the complete code to convert datetime to String object.

The output shows that “New Year Day already went by 11 days ago.”

top
X
Glad to have you at Our Website
Welcome to WPBot
wpChatIcon