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

Introduction to Python

Beginner
4 hr
4.9M
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

Pandas 2.0: What’s New and Top Tips

Dive into pandas 2.0, the latest update of the essential data analysis library, with new features like PyArrow integration, nullable data types, and non-nanosecond datetime resolution for better performance and efficiency.
Moez Ali's photo

Moez Ali

9 min

PyTorch 2.0 is Here: Everything We Know

Explore the latest release of PyTorch, which is faster, more Pythonic, and more dynamic.
Abid Ali Awan's photo

Abid Ali Awan

6 min

An Introduction to Python T-Tests

Learn how to perform t-tests in Python with this tutorial. Understand the different types of t-tests - one-sample test, two-sample test, paired t-test, and Welch’s test, and when to use them.
Vidhi Chugh's photo

Vidhi Chugh

13 min

Matplotlib time series line plot

This tutorial explores how to create and customize time series line plots in matplotlib.
Elena Kosourova's photo

Elena Kosourova

8 min

Step-by-Step Guide to Making Map in Python using Plotly Library

Make your data stand out with stunning maps created with Plotly in Python
Moez Ali's photo

Moez Ali

7 min

High Performance Data Manipulation in Python: pandas 2.0 vs. polars

Discover the main differences between Python’s pandas and polars libraries for data science
Javier Canales Luna's photo

Javier Canales Luna

16 min

See MoreSee More