datetimo 模块和time模块类似,只不过直接帮你定好了格式
import datetimetime =datetime.datetime.now()print(time,type(time))strtime =str(time)print(strtime[:strtime.find('.')],type(strtime))# 缺点格式固定了无法修改
# datatime模块import datetimenow_time = datetime.datetime.now() # 现在的时间# 只能调整的字段:weeks days hours minutes secondsprint(datetime.datetime.now() + datetime.timedelta(weeks=3)) # 三周后print(datetime.datetime.now() + datetime.timedelta(weeks=-3)) # 三周前print(datetime.datetime.now() + datetime.timedelta(days=-3)) # 三天前print(datetime.datetime.now() + datetime.timedelta(days=3)) # 三天后print(datetime.datetime.now() + datetime.timedelta(hours=5)) # 5小时后print(datetime.datetime.now() + datetime.timedelta(hours=-5)) # 5小时前print(datetime.datetime.now() + datetime.timedelta(minutes=-15)) # 15分钟前print(datetime.datetime.now() + datetime.timedelta(minutes=15)) # 15分钟后print(datetime.datetime.now() + datetime.timedelta(seconds=-70)) # 70秒前print(datetime.datetime.now() + datetime.timedelta(seconds=70)) # 70秒后current_time = datetime.datetime.now()# 可直接调整到指定的 年 月 日 时 分 秒 等print(current_time.replace(year=1977)) # 直接调整到1977年print(current_time.replace(month=1)) # 直接调整到1月份print(current_time.replace(year=1989,month=4,day=25)) # 1989-04-25 18:49:05.898601# 将时间戳转化成时间print(datetime.date.fromtimestamp(1232132131)) # 2009-01-17