Skip to main content
HomeTutorialsPython

Python Datetime Tutorial

Learn how to create a datetime object.
Updated 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.

Topics

Python Courses

Certification available

Course

Introduction to Python

4 hr
5.3M
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

Mastering the Pandas .explode() Method: A Comprehensive Guide

Learn all you need to know about the pandas .explode() method, covering single and multiple columns, handling nested data, and common pitfalls with practical Python code examples.
Adel Nehme's photo

Adel Nehme

5 min

Python NaN: 4 Ways to Check for Missing Values in Python

Explore 4 ways to detect NaN values in Python, using NumPy and Pandas. Learn key differences between NaN and None to clean and analyze data efficiently.
Adel Nehme's photo

Adel Nehme

5 min

Seaborn Heatmaps: A Guide to Data Visualization

Learn how to create eye-catching Seaborn heatmaps
Joleen Bothma's photo

Joleen Bothma

9 min

Test-Driven Development in Python: A Beginner's Guide

Dive into test-driven development (TDD) with our comprehensive Python tutorial. Learn how to write robust tests before coding with practical examples.
Amina Edmunds's photo

Amina Edmunds

7 min

Exponents in Python: A Comprehensive Guide for Beginners

Master exponents in Python using various methods, from built-in functions to powerful libraries like NumPy, and leverage them in real-world scenarios to gain a deeper understanding.
Satyam Tripathi's photo

Satyam Tripathi

9 min

Python Linked Lists: Tutorial With Examples

Learn everything you need to know about linked lists: when to use them, their types, and implementation in Python.
Natassha Selvaraj's photo

Natassha Selvaraj

9 min

See MoreSee More