Python datetime
Example 1: Get Current Date and Time
import datetime
datetime_object = datetime.datetime.now()
print(datetime_object)2018-12-19 09:26:03.478039Example 2: Get Current Date
import datetime
date_object = datetime.date.today()
print(date_object)datetime.date Class
Example 3: Date object to represent a date
Example 4: Get current date
Example 5: Get date from a timestamp
Example 6: Print today's year, month and day
datetime.time
Example 7: Time object to represent time
Example 8: Print hour, minute, second and microsecond
datetime.datetime
Example 9: Python datetime object
Example 10: Print year, month, hour, minute and timestamp
datetime.timedelta
Example 11: Difference between two dates and times
Example 12: Difference between two timedelta objects
Example 13: Printing negative timedelta object
Example 14: Time duration in seconds
Python format datetime
Python strftime() - datetime object to string
Example 15: Format date using strftime()
Python strptime() - string to datetime
Example 16: strptime()
Handling timezone in Python
Last updated