python 时间戳和日期相互转换
当前时间戳:time.time()
当前日期:time.ctime()
1、Python下日期到时间戳的转换
import datetime import time dateC=datetime.datetime(2010,6,6,8,14,59) timestamp=time.mktime(dateC.timetuple()) print timestamp
2、Python下将时间戳转换到日期
import datetime import time ltime=time.localtime(1395025933) timeStr=time.strftime("%Y-%m-%d %H:%M:%S", ltime) print timeStr
3、将前端传过来的时间字符串转为时间戳
import time a = "2011-09-28 10:00:00" time.mktime(time.strptime(a,'%Y-%m-%d %H:%M:%S')) #结果 1317175200
4、备注
"""
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst)
Convert seconds since the Epoch to a time tuple expressing local time.
When 'seconds' is not passed in, convert the current time instead.
"""
"""
strftime(format[, tuple]) -> string
Convert a time tuple to a string according to a format specification.
See the library reference manual for formatting codes. When the time tuple
is not present, current time as returned by localtime() is used.
"""