Arbeiten mit Datums- und Zeitangaben in Python
Max Shron
Data Scientist and Author
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']
# 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']
d.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
# Format: YYYY/MM/DD
print(d.strftime("%Y/%m/%d"))
2017/01/05
Arbeiten mit Datums- und Zeitangaben in Python