Python Date and Time: Modules and Functions

In Python, there are several ways to calculate date and time. There are some in-built modules used to determine the date and time. The name of such modules is given below which are used to determine date and time.

  • Module datetime
  • Module time
  • Module calendar

1. Python datetime module:

This module is used to work with both date and time. It is an in-built module. This module has different classes which are very much helpful to determine date and time. In python, datetime is an object and it is an immutable type.

Classes:

  • datetime.date: This class mainly follows the current Gregorian calendar and attributes of this class are year, month, and day.
  • datetime.time: This class assumes that each day of a year has 24*60*60 seconds and attributes of this class are the microsecond, second, minute, hour, and tzinfo.
  • datetime.datetime: It is used to determine both date and time and attributes are the microsecond, second, minute, hour, day, month, and year.
  • datetime.timedelta: This class is used to determine the difference between two different dates or two times and all this operation is done in a microsecond.
  • datetime.tzinfo: This is an abstract base class that is used to provide the information object about a different time zone.
  • datetime.timezone: This class implements the tzinfo class as a fixed offset from UTC.

Constants: The datetime module exports two types of constant. These two types of constants are:

  • Datetime.MINYEAR : This constant allows the minimum or the smallest number of years in a datetime object. By default, the value of MINYEAR is 1.
  • Datetime.MAXYEAR: This constant allows the maximum or the largest number of years in a datetime object. By default, the value of MAXYEAR is 9999.

2. Python time module:

This module is basically works on only time. The three main attributes of time are second, minute, and hour. This module has some built-in functions. The descriptions of those functions are given below.

List of functions:

  • Time.altzone:  This function is used basically only if the daylight is nonzero.
  • Time.asctime(tuple_time):  Convert the tuple time into the localtime() orgmtime() and return it.
  • Time.clock(): This function returns the CPU time.
  • Time.ctime(secs): Convert time from seconds to string format.
  • Time.gmtime(secs): This function converts a time expressed in second to a struct_time.
  • Time.localtime(secs): This function converts the time expressed in seconds to local time.
  • Time.mktime(tuple time): The function is nothing but a reverse function of localtime() function.
  • Time.sleep(secs): This function suspends the currently executing thread for the given number of times.
  • Time.strftime(format): Convert the tuple time into gmtime() or localtime() as a string format.
  • Time.strptime(string[,format]): This function parses a string time representation according to the given format. 
  • Time.time(): This function returns the time in seconds.
  • Time.tzset(): This function is used to reset the time conversion rule.

3. Python Calendar Module:

This module is also an inbuilt module in python that is used to determine calendars related problems. This module is very important and this module works on year, month, and days of a week. This module has some built-in functions. The descriptions of those functions are given below.

List of functions:

  • Calendar.calender(firstweekday=0): By default, 0 is Monday and 6 is Sunday. A Calendar object is used to format calendar data.
  • Calendar.firstweekday(): This function returns the current weekdays of each week.
  • Calendar.isleap(year): This function returns the boolean value that means true and false.
  • Calendar.leapdays(y1,y2): This function returns the number of leap years in the range from y1 to y2.
  • Calendar.month(year,month,w=0,l=0): This function returns a calendar of month using formatmonth().
  • Calendar.monthcalender(year,month): This function returns a matrix that represents the calendar of a month.
  • Calendar.monthrange(year,month): This function returns the number of days in a month and weekdays of a month.
  • Calendar.prcal(year,w=2,l=1,c=6): This function prints the calendar for an entire year.
  • Calendar.prmonth(year,month,w=2,l=1): This function prints a calendar of the month that is returned by month().
  • Calendar.setfirstweekday(weekday): This function is used to set the weekdays to start each week.
  • Calendar.timegm(tupletime): This function works on tuple time that is returned by gmtime().
  • Calendar.weekday(year,month,day): This function returns the day of the week for a particular year and month.