Python – Date & Time:

In python, datetime is a module which provides different classes to work with dates and times.

The types of objects in datetime are as below.

  1. date
  2. time
  3. datetime
  4. timedelta
  5. tzinfo
  6. timezone

Date Object:

Date object depicts the date as date(year, month, day) in an ideal Gregorian calendar. Syntax of the Date class is represented as below.

Syntax:

All the arguments are integers. Everyargumenthas its own range of values as below.

  1. YEAR: MINYEAR – MAXYEAR (1 – 9999)
  2. MONTH: 1 – 12
  3. DAY: 1 – number of days in the given month and year.

Now let us work with date object and its methods which serves different requirements with an example. The below example shows the current date in different formats.

Example:

Time object:

A time object which gives the information about time of any particular day subject to the requirements. The syntax of the time object constructor is given below.

Syntax:

  1. HOUR: 0 – 24
  2. MINUTE: 0 to < 60
  3. SECOND: 0 to < 60
  4. MICROSECOND: 0 to < 1000000
  5. fold in [0,1]

Example:

Date time Object:

Date time object is a combination of both date and time information which can provide the functions from date object and time object.

Syntax:

All the arguments are integers. Eachargumenthas its own range of values as below.

  1. YEAR: MINYEAR – MAXYEAR(1 – 9999)
  2. MONTH: 1 – 12
  3. DAY: 1 – number of days in the given month and year.
  4. HOUR: 0 – 24
  5. MINUTE: 0 to < 60
  6. SECOND: 0 to < 60
  7. MICROSECOND: 0 to < 1000000
  8. fold in [0,1]

Now let us work with datetime object and its methods which serves different requirements with an example.

Example: