Python Datetime Module
Python provides a huge collection of built-in modules that can be readily used while writing code in Python. A module is basically a collection of useful classes and each class has attributes and methods available for use. This helps developers to not reinvent the wheel for common tasks and focus on the big picture implementation. One such module that is quite useful is Datetime.
The Datetime module provides a collection of useful methods for working with date and time in Python. Let’s dive into this module and all the features it provides.
Some of the most common tasks you can do with datetime module are:
- Get current date and time
- Create new date if year, month and day are known
- Create time object if hour, minute, second is known
- Get date and time if Unix timestamp is known
We have barely touched the surface with these common tasks. Datetime is a great module with many other amazing features that comes handy for various tasks around date and time in Python.
Any built-in Python module can be used by first importing it at the top of the file using
Let’s see how to use the datetime module and it’s built-in features in Python.
A datetime object can also be created manually by specifying year, month, day, hour, minute, seconds and timestamp or a combination of these values. You can also find the difference between two time objects using timedelta.
Kick-Start Your Data Career
We offer a wide variety of programs and courses built on adaptive curriculum and led by leading industry experts.
- Work on projects in a collaborative setting
- Take advantage of our flexible plans and scholarships
- Get access to VIP events and workshops
The Python certificate course provides individuals with fundamental Python programming skills to effectively work with data.
Taught by data professionals working in the industry, the part-time Data Science course is built on a project-based learning model, which allows students to use data analysis, modeling, Python programming, and more to solve real analytical problems.
The Data Science bootcamp is an intensive course designed to launch students' careers in data.
The part-time Data Analytics course was designed to introduce students to the fundamentals of data analysis.