Python3.4时间格式化
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2016-05-09 23:00:38
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
python时间格式化为字符串和其它常用操作
#!/usr/bin/python # -*- coding: utf-8 -*- import time,datetime #输出当前时间秒数 curm=time.time() print('当前时间秒数:'+str(curm)) #取向前一天和向明天的日期 oneday = datetime.timedelta(days=1) today = datetime.date.today() yesterday = datetime.date.today() - oneday tomorrow = datetime.date.today() + oneday print('昨天:'+str(yesterday)) print('明天:'+str(tomorrow)) #取今天0点时间 today_zero_time = datetime.datetime.strftime(today, '%Y-%m-%d %H:%M:%S') print ('今天0点'+str(today_zero_time)) #取上个月最后一天 last_month_last_day = datetime.date(datetime.date.today().year,datetime.date.today().month,1)-datetime.timedelta(1) print('上个月最后一天'+str(last_month_last_day)) #把当前时间转为字符串日期 fname=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) print(fname) #把时间秒数转成日期 time_sec = 1369101035 fname=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_sec)) print(fname) #把日期转化为秒数 expire_time = "2013-05-21 09:50:35" d = datetime.datetime.strptime(expire_time,"%Y-%m-%d %H:%M:%S") time_sec_float = time.mktime(d.timetuple()) print (time_sec_float) #注意python里获取到的是float类型的秒数,而不是整形。如果要获得整数,可以用int(time_sec_float)
输出如下图