Direkt zum Inhalt

Python Datetime Tutorial

Learn how to create a datetime object.
5. Aug. 2020  · 2 Min. Lesezeit

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.

Themen

Python Courses

Kurs

Einführung in Python

4 Std.
6.5M
Lerne in nur vier Stunden die Grundlagen der Datenanalyse mit Python und entdecke beliebte Python-Pakete.
Siehe DetailsRight Arrow
Kurs starten
Mehr anzeigenRight Arrow
Verwandt
Working with Dates and Times.png

cheat-sheet

Working with Dates and Times in Python Cheat Sheet

Working with dates and times is essential when manipulating data in Python. Learn the basics of working with datetime data in this cheat sheet.
Richie Cotton's photo

Richie Cotton

Lernprogramm

Python String to DateTime: How to Convert Strings to DateTime Objects

Learn all about the Python datetime module in this step-by-step guide, which covers string-to-datetime conversion, code samples, and common errors.
Arunn Thevapalan's photo

Arunn Thevapalan

Lernprogramm

Python Dictionaries Tutorial

Learn how to create a dictionary in Python.
DataCamp Team's photo

DataCamp Team

Lernprogramm

Python String format() Tutorial

Learn about string formatting in Python.
DataCamp Team's photo

DataCamp Team

Lernprogramm

Tuples in Python Tutorial

Learn to use and unpack Tuples in Python.
DataCamp Team's photo

DataCamp Team

Lernprogramm

Pandas Drop Duplicates Tutorial

Learn how to drop duplicates in Python using pandas.
DataCamp Team's photo

DataCamp Team

Mehr anzeigenMehr anzeigen