python时间戳转换成时间_Python 时间戳/字符串/时间 转换
概要
平时对于时间的处理经常使用python的time和datetime模块,但是用来多次还是对其中的时间戳,字符串和时间转换应用的不太熟练,时间长了不使用就理不清楚,为此整理成文。
视图
时间戳,时间,字符串之间的关系整理如下图:
示例
时间戳和时间的转换
import time
if __name__ == "__main__":
# 时间戳: time.time() 返回当前时间戳
seconds = time.time()
# time.localtime()将时间戳转换为struct_time
s_time = time.localtime(seconds)
print s_time
# time.mktime()将struct_time转换为时间戳
print time.mktime(s_time)
1
2
3
4
5
6
7
8
9
10
11
12
13
# 输出 struct time: 包含年,月,日,小时,分钟,秒等
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=11, tm_hour=17, tm_min=31, tm_sec=57, tm_wday=5, tm_yday=223, tm_isdst=0)
# 时间戳
1533980060.0
1
2
3
4
5
时间和字符串之间的转换
import time
if __name__ == "__main__":
# time.strptime() 将字符串转换为struct_time
# %Y: 年
# %m: 月
# %d: 日
# %H: 时, %M:分, %S:秒, 更多详细需参考python time模块文档
s_time = time.strptime("2018-08-07", "%Y-%m-%d")
print s_time
# time.strftime()将struct_time转换为字符串
print time.strftime("%Y-%m-%d %H:%M:%S", s_time)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 输出
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=7, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=219, tm_isdst=-1)
2018-08-07 00:00:00
1
2
3
4
时间戳和字符串之间的转换
时间戳和字符串之间没有直接的转换方法,需要借助struct_time实现转换
import time
if __name__ == "__main__":
# 时间戳
seconds = time.time()
# 时间戳转换为字符串
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(seconds))
# 字符串转换为时间戳
print time.mktime(time.strptime("2018-08-07", "%Y-%m-%d"))
1
2
3
4
5
6
7
8
9
10
11
12
# 输出
2018-08-11 17:47:43
1533571200.0
---------------------
作者:回眸郎
python时间戳转换成时间_Python 时间戳/字符串/时间 转换相关推荐
- go json字符串转换成结构体,json字符串数组转换成切片
例子1:把字符串转换成结构体:{"FromUid":"100","FromName":"HANASHAN"} packa ...
- python将excel转换成图片_Python实现将Excel转换成为image的方法
我的主要思路是: Excel -> Html -> Image 代码如下: # -*- coding:utf-8 -*- __author__ = 'YangXin' import sys ...
- python 字符串比较时间_Python日期字符串比较
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 需要用python的脚本来快速检测一个文件内的二个时间日期字符串的大小,其实实现很简单,首先一些 ...
- python处理时间格式时分秒_python将时分秒转换成秒的实例
处理数据的时候遇到一个问题,从数据库里导出的数据是时分秒的格式:hh:mm:ss ,现在我需要把它转换成秒,方便计算. 原数据可能分两种情况,字段有可能是文本字符串类型的,也有可能是时间类型,他们的处 ...
- python 数字转化excel行列_Python 3 实现数字转换成Excel列名(10进制到26进制的转换函数)...
背景: 最近在看一些Python爬虫的相关知识,讲爬取的一些数据写入到Excel表中,当时当列的数目不确定的情况下,如何通过遍历的方式讲爬取的数据写入到Excel中. 开发环境: Python 3 ...
- angular 字符串转换成数字_Python基础语法大全:字符串的处理与使用
本文来自牛鹭学院学员:邓瑞杰 字母处理 string.capitalize() 返回元字符串,且将字符串第一个字母转为大写,其它字母小写 string.title() 返回元字符串,且将字符串第一个字 ...
- json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值...
json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值 原文:json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值 主要内容: 一 ...
- java 16进制数组 字符串_byte数组转换成16进制字符串和字符数组的方法
byte数组转换成16进制字符串String: public class CommonUtil { /** * byte数组转换成16进制字符串 * @param src * @return */ p ...
- 将字符串转换为日期,将日期转换成指定格式的字符串
/** * 将字符串转换为日期 * @param str * @return */ public static Date toDate(String str){ DateForm ...
- php如何将图片gif,jpg或mysql longblob或blob字段值转换成16进制字符串
php如何将图片gif,jpg或mysql longblob或blob字段值转换成16进制字符串!经过查找居然如此简单,一个函数就搞定了. 数据库脚本: -- -------------------- ...
最新文章
- Error: cannot allocate vector of size XX Gb
- 在Ubuntu虚拟机中安装VMware tools异常中断
- SAP OData:callbackend entry point - iwfndcl_mgw_request_manager~read_entity
- cpythonjavagolang_cpython:列表对象(PyListObject)的扩容机制
- golang jwt设置过期_听说你的JWT库用起来特别扭,推荐这款贼好用的!
- 如何使用JMeter 对Dubbo接口进行测试
- 基于流程管理的企业风险管理模式
- Android键盘删除键问题
- 福建福州软考考点安排在哪?
- arcgis怎么压缩tif文件_使用gdal压缩tif文件
- Teradata 数据库介绍
- 企业虚拟服务器管理,中小企业虚拟化解决方案-VMware vSphere 6.5-日常管理入口v0.0.1...
- 无法挂载sd卡(android),不能安装APK(1)
- 索尼mcx500切换台说明书_索尼MCX500特技切换台
- 【python 字符判断】python 判断名字是否含有数字,英文字符和汉字
- java判断浏览器杂项_java简单代码判断浏览器代码
- 阿里云中间件有哪些?这里最全面
- 新一代大学英语提高篇U校园答案(全)
- lv创建与挂载+oracle冷备恢复
- 深度学习pipeline和baseline是什么意思?