Skip to main content
HomeAbout PythonLearn Python

Python Datetime Tutorial

Learn how to create a datetime object.
Aug 2020  · 2 min read

Python Dates

Python has a special date class, called “date.” A date, like a string, or a number, or a numpy array, has special rules for creating it and methods for working with it.

Example of creating a datetime object

Here’s how to create a datetime object that computes the local time.

# Import datetime from the datetime module
from datetime import datetime

# Compute the local datetime: local_dt
local_dt = datetime.now()

# Print the local datetime
print(local_dt)

Try it for yourself.

Creating date objects

Example

# Import date
from datetime import date  

# Create Dates
two_hurricanes_dates = [date(2016, 10, 7), date(2017, 6, 21)]

Here we’ve created dates corresponding to two hurricanes, now as python date objects. The inputs to date(), are the year, month, and day. The first date is October 7, 2016 and the second date is June 21, 2017.

Attributes of a Date

You can access individual components of a date using the date’s attributes. You can access the year of the date using the year attribute, like so, and the result is 2016. Similarly, you can access the month and day using the month and day attributes.

Example

# Import date
from datetime import date

# Create Dates
two_hurricanes_dates = [date(2016, 10, 7), date(2017, 6, 21)]

print(two_hurricanes_dates[0].year)
print(two_hurricanes_dates[0].month)
print(two_hurricanes_dates[0].day)
2016
10
7

To learn more about datetime, please see this video from our course Working with Dates and Times in Python.

This content is taken from DataCamp’s course on Working with Dates and Times in Python by Max Shrom and Data Types for Data Science in Python by Jason Myers.

Check out our Converting Strings to Dates as datetime Objects tutorial.

Python Courses

Certification available

Introduction to Python

BeginnerSkill Level
4 hr
5.2M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

10 Essential Python Skills All Data Scientists Should Master

All data scientists need expertise in Python, but which skills are the most important for them to master? Find out the ten most vital Python skills in the latest rundown.

Thaylise Nakamoto

9 min

The 7 Best Python Certifications For All Levels

Find out whether a Python certification is right for you, what the best options are, and the alternatives on offer in this comprehensive guide.
Matt Crabtree's photo

Matt Crabtree

18 min

A Complete Guide to Socket Programming in Python

Learn the fundamentals of socket programming in Python
Serhii Orlivskyi's photo

Serhii Orlivskyi

41 min

Textacy: An Introduction to Text Data Cleaning and Normalization in Python

Discover how Textacy, a Python library, simplifies text data preprocessing for machine learning. Learn about its unique features like character normalization and data masking, and see how it compares to other libraries like NLTK and spaCy.

Mustafa El-Dalil

5 min

Coding Best Practices and Guidelines for Better Code

Learn coding best practices to improve your programming skills. Explore coding guidelines for collaboration, code structure, efficiency, and more.
Amberle McKee's photo

Amberle McKee

26 min

Pandas Profiling (ydata-profiling) in Python: A Guide for Beginners

Learn how to use the ydata-profiling library in Python to generate detailed reports for datasets with many features.
Satyam Tripathi's photo

Satyam Tripathi

9 min

See MoreSee More