python基础-datetime日期与时间

/ 0评 / 0
import datetime
 
# 获取当前时间
print(datetime.datetime.now())
print(datetime.datetime.today())
print(datetime.date.today())
 
# 时间格式化输出
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print(datetime.datetime.now().strftime("%Y%m%d"))
 
# 当前时间加减,正数加,负数减并转换字符串
print((datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime("%Y-%m-%d %H:%M:%S"))
print((datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S"))
print((datetime.datetime.now()+datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S"))
print((datetime.datetime.now()+datetime.timedelta(seconds=1)).strftime("%Y-%m-%d %H:%M:%S"))
print((datetime.datetime.now()+datetime.timedelta(weeks=1)).strftime("%Y-%m-%d %H:%M:%S"))
 
# 字符串转换日期对象
str1 = '2021-11-11 11:11:11'
date = datetime.datetime.strptime(str1, '%Y-%m-%d %H:%M:%S')
print(type(date))
 
# 时间比较
date1 = datetime.datetime.now()+datetime.timedelta(minutes=1)
date2 = datetime.datetime.now()
bool = date1 > date2
print(bool)

# 时间戳转换日期格式
timeStamp = 1644558590
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timeStamp))
print(date)

# 日期转换时间戳
datetime.datetime.now().timestamp()

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注