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:

class datetime.date(year,month,day)

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:

# need to import the date

>>> from datetime import date

# method today() shows the today's date

>>> today=date.today()

# let us see the date

>>> today

datetime.date(2017, 11, 7)

>>> today=x

>>> x

time.struct_time(tm_year=2017, tm_mon=8, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=227, tm_isdst=-1)

# assigning the date

>>> d=date(2017,11,7)

>>> x=d.timetuple()

# Print the date values from the tuple by year, month, day ..etc

>>> for i in x:

...     print(i)

...

2017

11

7

0

0

0

1                      # week day which is TUESDAY ( where '0' is MONDAY)

311                  # 311 th day in the year 2017

-1
# print the iso format of date

>>>d.isoformat()

'2017-11-07'

# String formats of date

>>>d.strftime("%d/%m/%u")

'07/11/2'

>>>d.strftime("%A%d.%B %Y")
'Tuesday07.November 2017'

>>> 'The {1} is {0:%d}, the {2} is {0:%B}.'.format(d, "day", "month")

'The day is 07, the month is November.'


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:

>>> from datetime import time

>>> t=time(12,12,12)

>>> t

datetime.time(12, 12, 12)

>>>t.isoformat()

'12:12:12'

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:

class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

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:

# import the datetime, date, time

>>> from datetime import datetime, date, time

# date

>>> d=date(2017,11,7)

# time

>>> t=time(10,10)

# combine both date and time

>>>datetime.combine(d,t)

datetime.datetime(2017, 11, 7, 10, 10)
# current date and time

>>>datetime.now()

datetime.datetime(2017, 11, 7, 17, 21, 24, 338804)

>>>datetime.utcnow()

datetime.datetime(2017, 11, 7, 11, 51, 37, 256627)

>>>dt=datetime.now()

>>>dt
datetime.datetime(2017, 11, 7, 17, 22, 58, 626832)

# displaying the time tuple

>>>tt=dt.timetuple()

>>>tt

time.struct_time(tm_year=2017, tm_mon=11, tm_mday=7, tm_hour=17, tm_min=22, tm_sec=58, tm_wday=1, tm_yday=311, tm_isdst=-1)
>>> for i in tt:

...     print(i)

...

2017    # year 

11        # month

7          # day

17        # hour
22        # minute

58        # second

1          # weekday ( 0 = Monday)

311      # 311 th day in the year 2017

-1

# string format of date and time

>>>dt.strftime("%A, %d. %B %Y %I:%M%p")

'Tuesday, 07. November 2017 05:22PM'

>>> 'The {1} is {0:%d}, the {2} is {0:%B}, the {3} is {0:%I:%M%p}.'.format(dt, "day", "month", "time")
'The day is 07, the month is November, the time is 05:22PM.'



>>>dt.isoformat()

'2017-11-07T17:22:58.626832'