Python Datetime

Veröffentlicht am: 04 März 2023
auf dem Kanal: Programming Academic
402
1

Python's datetime module provides classes for working with dates and times. Here's a brief introduction to some of the classes and functions in the module:

datetime: The datetime class is the most commonly used class in the datetime module. It represents a date and time.

from datetime import datetime
now = datetime.now() # current date and time
print(now)
date: The date class represents a date without a time component.


from datetime import date
today = date.today() # current date
print(today)
time: The time class represents a time without a date component.

from datetime import time
t = time(12, 30, 0) # 12:30:00
print(t)
timedelta: The timedelta class represents a duration between two dates or times.

from datetime import timedelta
delta = timedelta(days=1, hours=2, minutes=30) # 1 day, 2 hours, 30 minutes
print(delta)
strftime: The strftime() method is used to format a date or time object into a string.

from datetime import datetime
now = datetime.now()
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)
strptime: The strptime() function is used to parse a string into a date or time object.


from datetime import datetime
date_string = "2021-09-01"
date_object = datetime.strptime(date_string, "%Y-%m-%d")
print(date_object)
These are just some of the classes and functions available in the datetime module. For more information, you can refer to the Python documentation.


Auf dieser Seite können Sie das Online-Video Python Datetime mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Programming Academic 04 März 2023 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 402 Mal angesehen und es wurde von 1 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!