浅谈Python中用datetime包进行对时间的一些操作
1.计算给出两个时间之间的时间差
importdatetimeasdt #currenttime cur_time=dt.datetime.today() #oneday pre_time=dt.date(2016,5,20)#eg:2016.5.20 delta=cur_time-pre_time #ifyouwanttogetdiscrepancyindays printdelta.days #ifyouwanttogetdiscrepancyinhours printdelta.hours #andsoon
2.获取n天前的时间
cur_time=dt.now() #previousndays pre_time=dt.timedelta(days=n)
3.将给定的时间精确到天或者其他单位
cur_time=dt.now() #getdayofcurrenttime cur_day=cur_time.replace(hour=0,minute=0,second=0,mircrosecond=0)
4.获取一连串的时间序列(返回list)
cur_time=dt.datetime.today() datelist=[cur_time-dt.timedelta(days=x)forxinrange(0,100)]
或者
importpandasaspd datelist=pd.date_range(pd.datetime.today(),periods=100).tolist()
5.将时间字符串转化为datetime类型
date_formate="%Y-%m-%d"#year-month-day
time=dt.strptime('2016-06-22',date_format)
6.将时间类型转化为字符串类型
time_str=dt.strftime("%Y-%m-%d",dt.now())#returnlike"2016-06-22"
以上就是小编为大家带来的浅谈Python中用datetime包进行对时间的一些操作全部内容了,希望大家多多支持毛票票~
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短