Python DateTime module

The datetime classes are categorize into 6 main classes –

  • date
    • Its attributes are year, month and day
  • time
    • Its attributes are hour, minute, second, microsecond and tzinfo
  • datetime
    • Its a combination of date and time
    • With the attributes year, month, day, hour, minute, second, microsecond, and tzinfo.
  • timedelta
    • A duration expressing the difference between two date, time, or datetime instances to microsecond resolution.
  • tzinfo
    • It provides time zone information objects.
  • timezone
    • A class that implements the tzinfo abstract base class as a fixed offset from the UTC (New in version 3.2).

A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC

Widely used datetime methods

  • datetime.now() –> Get Current Date and Time
import datetime as dt
# Get current date and time
print(dt.datetime.now())
# 2021-08-31 08:46:31.958308

# Get current time
print(dt.datetime.now().strftime("%H:%M:%S"))
# 08:46:53
  • datetime.date() –> Create Date object to represent a date
  • datetime.date.today() –> Get Current Date
  • datetime.date.fromtimestamp –> Get date from a timestamp
import datetime as dt
# Get Current Date
to_day = dt.date.today()
print(to_day) 	
# 2021-08-31  --> YYYY-MM-DD

print(to_day.year, "--", to_day.month, '--', to_day.day)
# 2021 -- 8 -- 31

# Get date from a timestamp
print(dt.date.fromtimestamp(1326244364))
# 2012-01-11

# Create datetime obj
print(dt.date(2019, 4, 13))
# 2019-04-13
  • datetime.time() –> Create Time object to represent a time
import datetime as dt
print(dt.time()) 	# 00:00:00

print(dt.time(11, 34, 56))  
print(dt.time(hour = 11, minute = 34, second = 56))
# 11:34:56 --> HH:MM:SS

print(dt.time(11, 34, 56, 234566))  
# HH:MM:SS: microsecond

a = dt.time(11, 34, 56)
print(a.hour, a.minute, a.second, a.microsecond)
  • datetime.datetime() –> contain information from both date and time objects.
import datetime as dt
print(dt.datetime(2018, 11, 28))    
# 2018-11-28 00:00:00   --> YYYY:MM:DD HH:MM:SS:microsecond

print(dt.datetime(2017, 11, 28, 23, 55, 59, 342380))   
# 2017-11-28 23:55:59.342380

nw = dt.datetime.now()
print(nw.year,nw.month,nw.day, nw.hour,nw.minute,nw.second,nw.microsecond,nw.timestamp())
# 2021 7 28 10 30 29 722029 1627448429.722029

# Get from timestamp
print(dt.datetime.fromtimestamp(1627448429.722029))
# 2021-07-28 10:30:29.722029
  • datetime.timedelta() –> generally used for calculating differences in dates and also can be used for date manipulations
import datetime as dt
t1 = dt.date(year = 2018, month = 8, day = 12)
t2 = dt.date(year = 2018, month = 7, day = 5)
print(t1 - t2)   
# 38 days, 0:00:00

t3 = dt.datetime(year = 2019, month = 7, day = 12, hour = 7, minute = 9, second = 33)
t4 = dt.datetime(year = 2019, month = 6, day = 10, hour = 5, minute = 55, second = 13)
t5 = t3-t4

print(t5) 
# 32 days, 1:14:20

print(t5.days, t5.seconds)
# 32 4460

Python strptime() –> string to datetime

# %d, %B and %Y --> day, month(full name) and year
print(dt.datetime.strptime("21 June, 2018", "%d %B, %Y"))
# 2018-06-21 00:00:00

Python strftime() –> datetime object to string

# current date and time
now = dt.datetime.now()

print(now.strftime("%H:%M:%S"))  
# 22:23:47

print(now.strftime("%m/%d/%Y, %H:%M:%S"))
# 05/20/2021, 22:23:47   --> MM/DD/YYYY HH:MM:SS

print(now.strftime("%d/%m/%Y, %H:%M:%S"))
# 20/05/2021, 22:23:47    --> DD/MM/YYYY HH:MM:SS

Reference