Daten in Strings umwandeln

Arbeiten mit Datums- und Zeitangaben in Python

Max Shron

Data Scientist and Author

ISO-8601-Format

from datetime import date

# Beispiel-Datum
d = date(2017, 11, 5)

# ISO-Format: YYYY-MM-DD print(d)
2017-11-05
# Datum im ISO-8601-Format ausgeben und in eine Liste packen
print( [d.isoformat()] )
['2017-11-05']
Arbeiten mit Datums- und Zeitangaben in Python

ISO-8601-Format

# Ein paar Daten, mit denen Computer früher Probleme hatten
some_dates = ['2000-01-01', '1999-12-31']

# In Reihenfolge ausgeben print(sorted(some_dates))
['1999-12-31', '2000-01-01']
Arbeiten mit Datums- und Zeitangaben in Python

Alle anderen Formate

d.strftime()
Arbeiten mit Datums- und Zeitangaben in Python

Andere Formate: strftime

# Beispiel-Datum
d = date(2017, 1, 5)

print(d.strftime("%Y"))
2017
# Formatstring mit mehr Text
print(d.strftime("Year is %Y"))
Year is 2017
Arbeiten mit Datums- und Zeitangaben in Python

Andere Formate: strftime

# Format: YYYY/MM/DD
print(d.strftime("%Y/%m/%d"))
2017/01/05
Arbeiten mit Datums- und Zeitangaben in Python

Daten in Strings umwandeln

Arbeiten mit Datums- und Zeitangaben in Python

Preparing Video For Download...