Example 1: Get Current Date and Time

import datetime

datetime_object = datetime.datetime.now()
print(datetime_object)

When you run the program, the output will be something like:

2018-12-19 09:26:03.478039

Here, we have imported datetime module using import datetime statement.

One of the classes defined in the datetime module is datetime class. We then used now() method to create a datetime object containing the current local date and time.


Example 2: Get Current Date


import datetime

date_object = datetime.date.today()
print(date_object)

When you run the program, the output will be something like:

2018-12-19

In this program, we have used today() method defined in the date class to get a date object containing the current local date.


What’s inside datetime?

We can use dir() function to get a list containing all attributes of a module.

import datetime

print(dir(datetime))

When you run the program, the output will be:


['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_divide_and_round', 'date', 'datetime', 'datetime_CAPI', 'time', 'timedelta', 'timezone', 'tzinfo']

Commonly used classes in the datetime module are:

  • date Class
  • time Class
  • datetime Class
  • timedelta Class

datetime.date Class

You can instantiate date objects from the date class. A date object represents a date (year, month and day).


Example 3: Date object to represent a date


import datetime

d = datetime.date(2019, 4, 13)
print(d)

When you run the program, the output will be:

2019-04-13

If you are wondering, date() in the above example is a constructor of the date class. The constructor takes three arguments: year, month and day.

The variable a is a date object.


We can only import date class from the datetime module. Here’s how:


from datetime import date

a = date(2019, 4, 13)
print(a)

Example 4: Get current date

You can create a date object containing the current date by using a classmethod named today(). Here’s how:


from datetime import date

today = date.today()

print("Current date =", today)

Example 5: Get date from a timestamp

We can also create date objects from a timestamp. A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC. You can convert a timestamp to date using fromtimestamp() method.


from datetime import date

timestamp = date.fromtimestamp(1326244364)
print("Date =", timestamp)

When you run the program, the output will be:

Date = 2012-01-11

Example 6: Print today’s year, month and day

We can get year, month, day, day of the week etc. from the date object easily. Here’s how:


from datetime import date

# date object of today's date
today = date.today() 

print("Current year:", today.year)
print("Current month:", today.month)
print("Current day:", today.day)

datetime.time

A time object instantiated from the time class represents the local time.


Example 7: Time object to represent time


from datetime import time

# time(hour = 0, minute = 0, second = 0)
a = time()
print("a =", a)

# time(hour, minute and second)
b = time(11, 34, 56)
print("b =", b)

# time(hour, minute and second)
c = time(hour = 11, minute = 34, second = 56)
print("c =", c)

# time(hour, minute, second, microsecond)
d = time(11, 34, 56, 234566)
print("d =", d)

When you run the program, the output will be:


a = 00:00:00
b = 11:34:56
c = 11:34:56
d = 11:34:56.234566

Example 8: Print hour, minute, second and microsecond

Once you create a time object, you can easily print its attributes such as hourminute etc.


from datetime import time

a = time(11, 34, 56)

print("hour =", a.hour)
print("minute =", a.minute)
print("second =", a.second)
print("microsecond =", a.microsecond)

When you run the example, the output will be:


hour = 11
minute = 34
second = 56
microsecond = 0

Notice that we haven’t passed microsecond argument. Hence, its default value 0 is printed.


datetime.datetime

The datetime module has a class named dateclass that can contain information from both date and time objects.


Example 9: Python datetime object


from datetime import datetime

#datetime(year, month, day)
a = datetime(2018, 11, 28)
print(a)

# datetime(year, month, day, hour, minute, second, microsecond)
b = datetime(2017, 11, 28, 23, 55, 59, 342380)
print(b)

When you run the program, the output will be:


2018-11-28 00:00:00
2017-11-28 23:55:59.342380

The first three arguments yearmonth and day in the datetime() constructor are mandatory.


Example 10: Print year, month, hour, minute and timestamp


from datetime import datetime

a = datetime(2017, 11, 28, 23, 55, 59, 342380)
print("year =", a.year)
print("month =", a.month)
print("hour =", a.hour)
print("minute =", a.minute)
print("timestamp =", a.timestamp())

When you run the program, the output will be:


year = 2017
month = 11
day = 28
hour = 23
minute = 55
timestamp = 1511913359.34238

datetime.timedelta

timedelta object represents the difference between two dates or times.

Source: Python datetime (With Examples)