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

相关文章

浅谈numpy生成数组的零值问题

今天在用numpy写sinc函数时偶然发现在x=0时函数居然能取到1,觉得很不可思议,按理来说在x=0时函数无意义,研究了一下,发现竟然时numpy在生成数组时自动用一个很小的数代替了0...

PYTHON正则表达式 re模块使用说明

首先,运行 Python 解释器,导入 re 模块并编译一个 RE: #!python Python 2.2.2 (#1, Feb 10 2003, 12:57:01) >>...

Pycharm+django2.2+python3.6+MySQL实现简单的考试报名系统

Pycharm+django2.2+python3.6+MySQL实现简单的考试报名系统

1 准备工作 1.1 环境搭建 1.1.1 安装python3.6 python安装官网 1.1.2 安装django2.2 pip install django(==2.2.0) //...

深入浅析Python的类

面向对象编程时,都会遇到一个概念,类,python也有这个概念,下面我们通过代码来深入了解下。 创建和使用类 class Dog(): def __init__(self, n...

Python实现读写INI配置文件的方法示例

本文实例讲述了Python实现读写INI配置文件的方法。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- import ConfigParser im...