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)
输出如下图