Date Time Functions
Python has a module named datetime to work with dates and times. Let's create a few simple programs related to date and time before we dig deeper.
Example 1: Get Current Date and Time
import datetime
datetime_object = datetime.datetime.now()
print(datetime_object)
2019-05-19 09:26:03.478039
Example 2: Get Current Date
import datetime
date_object = datetime.date.today()
print(date_object)
2019-05-19
Example 3: Date object to represent a date
import datetime
d = datetime.date(2019, 4, 13)
print(d)
2019-04-13
Example 4: Get current date
from datetime import date
today = date.today()
print("Current date =", today)
2019-05-19
Timestamp is the number of seconds between a particular date and January 1, 1970 at UTC
1 day = 86400 seconds
10 day = 864000 seconds
100 day = 8640000 seconds
Example 5: Get date from a timestamp
from datetime import date
timestamp = date.fromtimestamp(1326244364)
print("Date =", timestamp)
Date = 2012-01-11
Example 6: Print today's year, month and day
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)
Current year: 2019
Current month: 5
Current day: 19
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)
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
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)
hour = 11
minute = 34
second = 56
microsecond = 0
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)
2018-11-28 00:00:00
2017-11-28 23:55:59.342380
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())
year = 2017
month = 11
day = 28
hour = 23
minute = 55
timestamp = 1511913359.34238
Example 11: Difference between two dates and times
from datetime import datetime, date
t1 = date(year = 2018, month = 7, day = 12)
t2 = date(year = 2017, month = 12, day = 23)
t3 = t1 - t2
print("t3 =", t3)
t4 = datetime(year = 2018, month = 7, day = 12, hour = 7, minute = 9, second = 33)
t5 = datetime(year = 2019, month = 6, day = 10, hour = 5, minute = 55, second = 13)
t6 = t4 - t5
print("t6 =", t6)
print("type of t3 =", type(t3))
print("type of t6 =", type(t6))
t3 = 201 days, 0:00:00
t6 = -333 days, 1:14:20
type of t3 = <class 'datetime.timedelta'>
type of t6 = <class 'datetime.timedelta'>
Example 12: Difference between two timedelta objects
from datetime import timedelta
t1 = timedelta(weeks = 2, days = 5, hours = 1, seconds = 33)
t2 = timedelta(days = 4, hours = 11, minutes = 4, seconds = 54)
t3 = t1 - t2
print("t3 =", t3)
t3 = 14 days, 13:55:39
Example 13: Printing negative timedelta object
from datetime import timedelta
t1 = timedelta(seconds = 33)
t2 = timedelta(seconds = 54)
t3 = t1 - t2
print("t3 =", t3)
print("t3 =", abs(t3))
t3 = -1 day, 23:59:39
t3 = 0:00:21
Handling timezone in Python
from datetime import datetime
import pytz
local = datetime.now()
print("Local:", local.strftime("%m/%d/%Y, %H:%M:%S"))
tz_NY = pytz.timezone('America/New_York')
datetime_NY = datetime.now(tz_NY)
print("NY:", datetime_NY.strftime("%m/%d/%Y, %H:%M:%S"))
tz_London = pytz.timezone('Europe/London')
datetime_London = datetime.now(tz_London)
print("London:", datetime_London.strftime("%m/%d/%Y, %H:%M:%S"))
Local: 04/23/2020, 12:00:11
NY: 04/23/2020, 02:30:11
London: 04/23/2020, 07:30:11
Last updated
Was this helpful?