Python Tutorial Python Advanced Python References Python Libraries

Python - Date & Time



Python has a built-in module called datetime to work with dates and times. To use the module, it must be imported in the current script.

datetime class of datetime module

One of the class defined in datetime module is datetime class. The datetime class contains now() method which is used to get current time and date. This class is used when it is required to work with dates and times both.

Get current date and time

In the example below, datetime module is imported in the current script and renamed as dt. The now() of datetime class is used to get current time and date. The output contains year, month, day, hour, minute, second, and microsecond.

import datetime as dt
x = dt.datetime.now()
print(x)

The output of the above code will be:

2019-11-19 17:47:49.843822

Create datetime object

As discussed earlier, a datetime object contains information about year, month, day, hour, minute, second, and microsecond. It can be created by user by passing arguments in datetime class constructor. See the example below for syntax.

import datetime as dt
x = dt.datetime(2019, 10, 15)
print(x)

y = dt.datetime(2019, 10, 15, 10, 15, 25, 111)
print(y)

The output of the above code will be:

2019-10-15 00:00:00
2019-10-15 10:15:25.000111

Use datetime object

A datetime object can be used to find out year, month, day, hour, minute, second, and microsecond separately for a particular date as shown in the example below.

import datetime as dt
x = dt.datetime(2019, 10, 15, 10, 15, 25, 111)

print("year:", x.year)
print("month:", x.month)
print("day:", x.day)
print("hour:", x.hour)
print("minute:", x.minute)
print("second:", x.second)
print("microsecond:", x.microsecond)

The output of the above code will be:

year: 2019
month: 10
day: 15
hour: 10
minute: 15
second: 25
microsecond: 111

date class of datetime module

Another class defined in datetime module is date class. The date class contains today() method which can be used to get current date. This class is used when it is required to work with dates only.

Get current date

The today() method of date class is used to get current date. The output contains year, month, and day.

import datetime as dt
x = dt.date.today()
print(x)

The output of the above code will be:

2019-11-19

Create date object

As discussed earlier, a date object contains information about year, month, and day. A date object can be created by user by passing arguments in date class constructor. See the example below for syntax.

import datetime as dt
x = dt.date(2019, 10, 15)
print(x)

The output of the above code will be:

2019-10-15

Use date object

A date object can be used to find out year, month, and day separately for a particular date as shown in the example below.

import datetime as dt
x = dt.date(2019, 10, 15)

print("year:", x.year)
print("month:", x.month)
print("day:", x.day)

The output of the above code will be:

year: 2019
month: 10
day: 15

time class of datetime module

Another class defined in datetime module is time class. The time class used when it is required to work with time only.

Create time object

A time object contains information about hour, minute, second, and microsecond. It can be created by user by passing arguments in time class constructor. See the example below for syntax.

import datetime as dt
x = dt.time(10, 15, 25)
print(x)

y = dt.time(9, 18, 27, 111)
print(y)

The output of the above code will be:

10:15:25
09:18:27.000111

Use time object

A time object can be used to find out hour, minute, second, and microsecond separately for a particular time as shown in the example below.

import datetime as dt
x = dt.time(10, 15, 25, 111)

print("hour:", x.hour)
print("minute:", x.minute)
print("second:", x.second)
print("microsecond:", x.microsecond)

The output of the above code will be:

hour: 10
minute: 15
second: 25
microsecond: 111

timedelta class of datetime module

Difference of two datetime objects or date objects creates a timedelta object.

import datetime as dt
x1 = dt.datetime(year=2019, month=11, day=10, hour=10, minute=15, second=20)
x2 = dt.datetime(year=2018, month=6, day=5, hour=5, minute=10, second=15)
t1 = x1 - x2

y1 = dt.date(year = 2019, month =11, day = 10)
y2 = dt.date(year = 2018, month = 6, day = 5)
t2 = y1 - y2

print(t1)
print("type of t1:",type(t1)) 
print(t2)
print("type of t2:",type(t2)) 

The output of the above code will be:

523 days, 5:05:05
type of t1: <class 'datetime.timedelta'>
523 days, 0:00:00
type of t2: <class 'datetime.timedelta'>

A timedelta object can also be created using keywords as shown in the example below. Difference of two timedelta objects is again a timedelta object. Along with this, it can be converted into seconds.

import datetime as dt
x = dt.timedelta(weeks = 10, days = 10, hours = 10, minutes = 15, seconds = 20)
y = dt.timedelta(weeks = 10)
t = x - y
print(t)
print(t.seconds)

The output of the above code will be:

10 days, 10:15:20
36920

The strftime() Method

The strftime method is defined under datetime, date and time classes of datetime module. It is used to format datetime, date or time objects in more readable format.

import datetime as dt
x = dt.datetime(2019, 10, 5, 10, 15, 20)
print("Formatting a datetime object:")
print(x.strftime("%d/%m/%Y"))
print(x.strftime("%d %B,%Y"))
print(x.strftime("%d %B,%Y  %H:%M:%S"))

y = dt.date(2019, 10, 5)
print("\nFormatting a date object:")
print(y.strftime("%d/%m/%Y"))
print(y.strftime("%d %B,%Y"))

z = dt.time(10, 15, 20)
print("\nFormatting a time object:")
print(z.strftime("%H:%M:%S"))
print(z.strftime("%H hours %M minutes %S seconds"))

The output of the above code will be:

Formatting a datetime object:
05/10/2019
05 October,2019
05 October,2019  10:15:20

Formatting a date object:
05/10/2019
05 October,2019

Formatting a time object:
10:15:20
10 hours 15 minutes 20 seconds

The strftime() formats discussed in above example have following interpretation.

  • %Y - Year, full version - (0001, ..., 2019, ..., 9999)
  • %b - Month name, short version - (Jan, ..., Dec)
  • %B - Month name, full version - (January, ..., December)
  • %m - Month in a number - (01, ..., 12)
  • %d - Day of month - (01, ..., 31)
  • %H - Hour - (00, 01, ..., 23)
  • %M - Minute - (00, 01, ..., 59)
  • %S - Second - (00, 01, ..., 59)

To learn more about strftime(), please visit - Python strftime() Method.

The strptime() Method

The strptime method is defined under datetime class of datetime module. It is used to convert string into a datetime object. To convert a string into datetime object, it need to be in certain format.

import datetime as dt

MyString = "25 Oct, 2019"
x = dt.datetime.strptime(MyString, "%d %b, %Y")
print(x)

MyString = "24/9/2018"
x = dt.datetime.strptime(MyString, "%d/%m/%Y")
print(x)

MyString = "23/8/2017 10:15:20"
x = dt.datetime.strptime(MyString, "%d/%m/%Y %H:%M:%S")
print(x)

MyString = "22/7/2016 9 hr 18 min 27 sec"
x = dt.datetime.strptime(MyString, "%d/%m/%Y %H hr %M min %S sec")
print(x)

The output of the above code will be:

2019-10-25 00:00:00
2018-09-24 00:00:00
2017-08-23 10:15:20
2016-07-22 09:18:27

To learn more about strptime(), please visit - Python strptime() Method.



More Concepts on Python Dates