Python的datetime模塊提供了處理日期和時(shí)間的功能。以下是一些常用的方法和示例:
import datetime
now = datetime.datetime.now()
print(now)
date = datetime.datetime(2021, 10, 31, 12, 30, 45)
print(date)
formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_date)
year = now.year
month = now.month
day = now.day
hour = now.hour
minute = now.minute
second = now.second
print(year, month, day, hour, minute, second)
date1 = datetime.datetime(2021, 10, 31)
date2 = datetime.datetime(2021, 11, 10)
delta = date2 - date1
print(delta.days)
這些是常用的datetime函數(shù)的用法,希望對(duì)你有幫助。