1.年月日,时分秒,这是‘str’类型
date = datetime.now().strftime('%Y-%m-%d')
now= datetime.now().strftime('%H:%M:%S')
2.将其转化为‘datetime.datetime’类型才能相减算时间差,a-b的类型为‘datetime.timedelta’
date=datetime.strptime(date,'%Y-%m-%d')
a=datetime.strptime(now1,'%H:%M:%S')
b=datetime.strptime(now2,'%H:%M:%S')
print(type(a-b))
3.将时间类型插入数据库,无论是‘str’还是‘‘datetime.datetime’类型,都可以插入数据库
cursor.execute("UPDATE work SET off=%s , deltime=%s where username=%s and dates=%s",
((now2),(b-a),user_name,date))
4.设置数据库中时间类型为datetime,则显示日期和时间
5.设置数据库中时间类型为date,则只显示日期
6.设置数据库中时间类型为time,则只显示具体时间点