python unix时间戳与正常时间转化
2016-04-10 23:12
543 查看
有时候业务需要,需要把正常的时间格式与unix时间戳格式进行转换。
在python中转化方式如下,直接利用time中的函数:
#! /usr/bin/env python
#coding:utf-8
import sys,os,re
import time
def timestamp_datatime(value):
format = '%Y-%m-%d %H:%M'
#format = '%Y-%m-%d %H:%M:%S'
#value 为时间戳值,如:1460073600.0
value = time.localtime(value)
dt = time.strftime(format,value)
return dt
def datetime_timestamp(dt):
time.strptime(dt,'%Y-%m-%d %H:%M')
s = time.mktime(time.strptime(dt,'%Y-%m-%d %H:%M'))
return s
if __name__ == '__main__':
d = datetime_timestamp('2016-04-08 08:00')
print d
s = timestamp_datatime(1460073600.0)
print s
在python中转化方式如下,直接利用time中的函数:
#! /usr/bin/env python
#coding:utf-8
import sys,os,re
import time
def timestamp_datatime(value):
format = '%Y-%m-%d %H:%M'
#format = '%Y-%m-%d %H:%M:%S'
#value 为时间戳值,如:1460073600.0
value = time.localtime(value)
dt = time.strftime(format,value)
return dt
def datetime_timestamp(dt):
time.strptime(dt,'%Y-%m-%d %H:%M')
s = time.mktime(time.strptime(dt,'%Y-%m-%d %H:%M'))
return s
if __name__ == '__main__':
d = datetime_timestamp('2016-04-08 08:00')
print d
s = timestamp_datatime(1460073600.0)
print s
相关文章推荐
- python操作excle
- Python学习之路--select解析
- 初级cnn研究辅助:python的matplotlib显示图片 之 按钮和触发事件
- python+opencv目标匹配技术
- 《Python核心编程》第十二章:模块
- Python中strip(),lstrip(), rstrip()使用方法
- K-近邻算法
- 我的Python编程体验总结
- 初级cnn研究辅助:python的matplotlib显示图片
- python爬虫学习之抓取一个页面
- 用Python写一个文本转HTML的脚本
- Python使用MySQL数据库
- python中的字符串
- Python的内置模块math介绍
- Head First Python 学习札记 2016-04-10
- python比较文本相似度(not semantical)
- Python
- python中subprocess.Popen.poll
- 【python】matplotlib生成散点矩阵图
- python的逻辑语句