Python Date and Time

Python provides three modules: datetime, time and calendar that help us to deal with dates, times, durations and calendars. The recent module is the 'datetime' module that deals with dates along with times. The 'datetime' module contains four important classes by the names 'datetime', 'date', 'time' and 'timedelta'.

  • The datetime class handles a combination of date and time.It contains attributes: year, month, day, hour, minute, second, microsecond, and tzinfo.
  • The date class handles dates of Gregorian calendar, without taking time zone into consideration.It has the attributes: year, month, and day.
  • The time class handles time assuming that every day has exactly 24 X 60 X 60 seconds.Its attributes: hour, minute, second, microsecond, and tzinfo.
  • The timedelta class is useful to handle the durations.The duration may be the difference between two date, time, or datetime instances.