Python 获取当前时间、格式化与简单计算
使用datetime
import datetime
print(datetime.date.today())
print(datetime.datetime.now())
print(datetime.datetime.now().weekday())
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
parse_date = datetime.datetime.strptime('2024-01-27', '%Y-%m-%d')
print(parse_date.year, parse_date.month, parse_date.day)
parse_date = datetime.datetime.strptime('2024-01-27', '%Y-%m-%d')
after_date = parse_date + datetime.timedelta(days=1)
print((after_date - parse_date).days)
使用time
import time
print(time.time())
current_time = time.localtime()
print(current_time.tm_year)
print(current_time.tm_mon)
print(current_time.tm_mday)
print(current_time.tm_wday)
print(current_time.tm_yday)
print(time.strftime('%Y-%m-%d %H:%M:%S',))