Introdcution
Datetime module in Python supplies classes for date and time manipulation. Commonly used classes in the datetime module are:
- date Class
- time Class
- datetime Class
- timedelta Class
Below example gets the today’s date and time.
import datetime # Current Date and Time object = datetime.datetime.now() # Today's Date date = datetime.date.today()
Date Class
A date object represents a date (year, month and day). 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.
from datetime import date # Date object, Year, Month, Date d = datetime.date(2019, 4, 13) # Today's Date today = date.today() # Date from a timestamp timestamp = date.fromtimestamp(1326244364) # Today's year, month and day print("Current year:", today.year) print("Current month:", today.month) print("Current day:", today.day) # Modify values of date object d = d.replace(month=10, day=13)
Time class
A time object represents a time (hour, minute, second and microsecond). A time object instantiated from the time class represents the local time.
from datetime import time # time(hour = 0, minute = 0, second = 0) a = time() # time(hour, minute and second) b = time(11, 34, 56) # time(hour, minute, second, microsecond) d = time(11, 34, 56, 234566) # Hour, minute, second and microsecond print("hour =", a.hour) print("minute =", a.minute) print("second =", a.second) print("microsecond =", a.microsecond) # Modify values of time objects a = a.replace(hour=5)
Datetime class
A datetime object is a single object containing all the information from a date object and a time object. datetime module has a class named datetime that can contain information about both date and time.
from datetime import datetime #datetime(year, month, day) a = datetime(2018, 11, 28) # datetime(year, month, day, hour, minute, second, microsecond) b = datetime(2017, 11, 28, 23, 55, 59, 342380) # year, month, hour, minute and timestamp print("year =", a.year) print("month =", a.month) print("hour =", a.hour) print("minute =", a.minute) print("timestamp =", a.timestamp()) # Modify values of datetime objects a = a.replace(year=2057, month=12)
Timedelta class
A timedelta object represents the difference between two dates or times.
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 # Duration in seconds print("total seconds =", t3.total_seconds()) # Date manipulation now = datetime.datetime.now() oneyear = datetime.timedelta(days=365) oneweek = datetime.timedelta(weeks=1) print("One year from now : ", now + oneyear) print("One week from now : ", now + oneweek) print("One week ago was : ", now - oneweek)
strftime()
This method is defined under classes date, datetime and time. It creates a formatted string from a given date, datetime or time object. The strftime() method takes one or more format codes and returns a formatted string based on it. Format codes are
- %Y – year [0001,…, 2018, 2019,…, 9999]
- %m – month [01, 02, …, 11, 12]
- %d – day [01, 02, …, 30, 31]
- %H – hour [00, 01, …, 22, 23
- %M – minute [00, 01, …, 58, 59]
- %S – second [00, 01, …, 58, 59]
from datetime import datetime # Current date and time now = datetime.now() s1 = now.strftime("%H:%M:%S") s2 = now.strftime("%m/%d/%Y, %H:%M:%S") # Format day and month print(now.strftime("%a, %A, %w, %e")) print(now.strftime("%b, %B, %m")) # Format time print(now.strftime("%H, %I, %M, %S, %p")) # Format short date format (m/d/y) output = now.strftime("%m/%d/%y") # Format long date format (Day, number, Month, Year) output = now.strftime("%A %d, %B %Y") # Format short date and time (m/d/y, H:M am/pm) output = now.strftime("%m/%d/%y %I:%M %p")
strptime()
The strptime() method creates a datetime object from a given string (representing date and time). The strptime() method takes two arguments:
- String representing date and time
- Format code equivalent to the first argument
from datetime import datetime date_string = "21 June, 2018" date_object = datetime.strptime(date_string, "%d %B, %Y") print("date_object =", date_object)