Python时间戳与时间字符串互相转换实例代码

yipeiwu_com6年前Python基础

复制代码 代码如下:

#设a为字符串
import time
a = "2011-09-28 10:00:00"

#中间过程,一般都需要将字符串转化为时间数组
time.strptime(a,'%Y-%m-%d %H:%M:%S')
>>time.struct_time(tm_year=2011, tm_mon=9, tm_mday=27, tm_hour=10, tm_min=50, tm_sec=0, tm_wday=1, tm_yday=270, tm_isdst=-1)

#将"2011-09-28 10:00:00"转化为时间戳
time.mktime(time.strptime(a,'%Y-%m-%d %H:%M:%S'))
>>1317091800.0

#将时间戳转化为localtime
x = time.localtime(1317091800.0)
time.strftime('%Y-%m-%d %H:%M:%S',x)
>>2011-09-27 10:50:00

相关文章

将python2.7添加进64位系统的注册表方式

解决问题:python2.7无法在注册表中被识别,即在安装NumPy和SciPy等出现“python version 2.7 required, which was not found...

Python计算时间间隔(精确到微妙)的代码实例

Python计算时间间隔(精确到微妙)的代码实例

使用python中的datetime import datetime oldtime=datetime.datetime.now() print oldtime; x=1 while...

详解Django中CBV(Class Base Views)模型源码分析

详解Django中CBV(Class Base Views)模型源码分析

在view文件中编写一个类,并配置好路由 class Test(View): def get(self, request, *args, **kwargs): return Ht...

python切片的步进、添加、连接简单操作示例

本文实例讲述了python切片的步进、添加、连接简单操作。分享给大家供大家参考,具体如下: 步进切片: #coding:utf-8 a="123456" print a[::-1]...

python中如何使用正则表达式的非贪婪模式示例

前言 本文主要给大家介绍了关于python使用正则表达式的非贪婪模式的相关内容,分享出来供大家参考学习,下面话不多说了,来一起详细的介绍吧。 在正则表达式里,什么是正则表达式的贪婪与非贪...