datetime格式转换:
时间格式转化strptime/字符串格式转换strftime函数
from dateutil.parser import parse
1432196564000
13位时间戳,年月日,时分秒,毫级
转为正常,则除以1000。即十位数值为年月日时分秒
当前时间戳:time.time()
当前日期:time.ctime()
- 将字符串的时间转换为时间戳
a = “2013-10-10 23:40:00” 将其转换为时间数组
import time
timeArray = time.strptime(a, “%Y-%m-%d %H:%M:%S”)
转换为时间戳:
timeStamp = int(time.mktime(timeArray))
timeStamp = 1381419600 - 时间戳转换为指定格式日期:
1: 利用localtime()转换为时间数组,然后格式化为需要的格式,如
importtime
tabc = 1432196564000
timeArray = time.localtime(abc/1000)
item[‘date’] = time.strftime(“%Y-%m-%d %H:%M:%S”, timeArray)
2: import datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime(“%Y-%m-%d %H:%M:%S”)
otherStyletime = “2013-10-10 23:40:00” 获取当前时间并转换为指定日期格式
1: import time
获得当前时间时间戳
now = int(time.time()) ->这是时间戳
转换为其他日期格式,如:”%Y-%m-%d %H:%M:%S”
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime(“%Y-%m-%d %H:%M:%S”, timeArray)
2: now = datetime.datetime.now() ->这是时间数组格式
转换为指定的格式:
otherStyleTime = now.strftime(“%Y-%m-%d %H:%M:%S”)字符串格式更改
先转为时间数组,然后转为其他格式
timeArray = time.strptime(a, “%Y-%m-%d %H:%M:%S”)
otherStyleTime = time.strftime(“%Y/%m/%d %H:%M:%S”, timeArray)
参考链接:http://www.2cto.com/kf/201401/276088.html
http://blog.sina.com.cn/s/blog_771875550101jfw2.html