Python datetime formats and functions
In this post, you will learn about the datetime formats and functions of the Python programming language.
In any programming language, datetime is a common datatype. Date and Time are often written in strings or any other format that we should convert into a single format. Python provides a standard library for date and time formats. Datetime module of Python supplies classes to work with date and time. These classes provide a number of functions to deal with dates, times, and time intervals. The datetime and time modules are mostly used.
Here is a simple Python program to import the datetime module and display the current date and time.
from datetime import datetime
now = datetime.now()
print(now)
When you execute the above code, it returns the date and time in the following format.
2020-04-11 11:59:17.164626
Types of datetime module
date | Store calendar date as year, month, day using the Gregorian calendar. |
time | Store time of day as hours, minutes, seconds, and microseconds. |
datetime | Store both date and time. |
timedelta | Represents the difference between two datetime values. |
Get Module Attribute
We can get list of all attributes of any of the above modules using dir() method.
import datetime
print(dir(datetime))
The above code returns all the possible attributes of the specified module.
['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']
Python Datetime
Create Datetime Object
The datetime module provides the datetime() class to create a date. It requires three parameters "year, month, day" to create a date.
from datetime import datetime
now = datetime(2018, 11, 4)
print(now.year, now.month, now.day)
Output
2018 11 4
It also taken optional parameters for time and timezone.
from datetime import datetime
now = datetime(2019, 11, 4, 22, 9, 11, 832092)
print(now.year, now.month, now.day)
Output
2019 11 4
Python timedelta
A timedelta object represents a duration, the difference between two dates or times.
from datetime import timedelta
delta = timedelta(2020, 4, 11) - timedelta(2010, 5, 23, 7, 15)
print(delta.days)
print(delta.seconds)
Output
9
85498
Python Date
Python provides a date class from which we can instantiate a date object. It represents the date in the format (year, month and day).
import datetime
d = datetime.date(2020, 1, 10)
print(d)
Output
2020-01-10
We can also create a date object and use the class method today() to get the current date.
from datetime import date
today = date.today()
print(today)
This method provides different attributes to get year, month, day individually.
from datetime import date
today = date.today()
print("Current Year: ",today.year)
print("Current Month: ",today.month)
print("Current Day: ",today.day)
Output
Current Year: 2020
Current Month: 4
Current Day: 13
Python Time
Python provides a time class from which we can instantiate a time object. It represents the local 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)
Get Date From Timestamp
We can create a date object from a timestamp. Python provides the fromtimestamp() method to convert a timestamp to date.
from datetime import date
timestamp = date.fromtimestamp(1516244423)
print("Date =", timestamp)
Output
Date = 2018-01-18
Related Articles
Python web scraping using urllibPython requests GET method
How to convert MySQL query result to JSON in Python
How to display data from MongoDB in HTML table using Python
CRUD operations in Python using MongoDB connector
Write Python Pandas Dataframe to CSV
Quick Introduction to Python Pandas
Python Pandas DataFrame
Python3 Tkinter Messagebox
Python get visitor information by IP address
Python Webbrowser
Python Tkinter Overview and Examples
Python Turtle Graphics Overview
OpenCV color detection
OpenCV bitwise and
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images