Python之time模块的时间戳,时间字符串格式化与转换方法(13位时间戳)


Posted in Python onAugust 12, 2019

Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。

关于时间戳的几个概念

时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。

时间元组(struct_time),包含9个元素。

time.struct_time(tm_year=2017, tm_mon=10, tm_mday=1, tm_hour=14, tm_min=21, tm_sec=57, tm_wday=6, tm_yday=274, tm_isdst=0)

时间格式字符串,字符串形式的时间。

time模块与时间戳和时间相关的重要函数

time.time() 生成当前的时间戳,格式为10位整数的浮点数。

time.strftime()根据时间元组生成时间格式化字符串。

time.strptime()根据时间格式化字符串生成时间元组。time.strptime()与time.strftime()为互操作。

time.localtime()根据时间戳生成当前时区的时间元组。

time.mktime()根据时间元组生成时间戳。

示例

关于时间戳和格式化字符串的简单示例如下

import time

#生成当前时间的时间戳,只有一个参数即时间戳的位数,默认为10位,输入位数即生成相应位数的时间戳,比如可以生成常用的13位时间戳
def now_to_timestamp(digits = 10):
 time_stamp = time.time()
 digits = 10 ** (digits -10)
 time_stamp = int(round(time_stamp*digits))
 return time_stamp

#将时间戳规范为10位时间戳
def timestamp_to_timestamp10(time_stamp):
 time_stamp = int (time_stamp* (10 ** (10-len(str(time_stamp)))))
 return time_stamp

#将当前时间转换为时间字符串,默认为2017-10-01 13:37:04格式
def now_to_date(format_string="%Y-%m-%d %H:%M:%S"):
 time_stamp = int(time.time())
 time_array = time.localtime(time_stamp)
 str_date = time.strftime(format_string, time_array)
 return str_date

#将10位时间戳转换为时间字符串,默认为2017-10-01 13:37:04格式
def timestamp_to_date(time_stamp, format_string="%Y-%m-%d %H:%M:%S"):
 time_array = time.localtime(time_stamp)
 str_date = time.strftime(format_string, time_array)
 return str_date

#将时间字符串转换为10位时间戳,时间字符串默认为2017-10-01 13:37:04格式
def date_to_timestamp(date, format_string="%Y-%m-%d %H:%M:%S"):
 time_array = time.strptime(date, format_string)
 time_stamp = int(time.mktime(time_array))
 return time_stamp

#不同时间格式字符串的转换
def date_style_transfomation(date, format_string1="%Y-%m-%d %H:%M:%S",format_string2="%Y-%m-%d %H-%M-%S"):
 time_array = time.strptime(date, format_string1)
 str_date = time.strftime(format_string2, time_array)
 return str_date

实验

print(now_to_date())
print(timestamp_to_date(1506816572))
print(date_to_timestamp('2017-10-01 08:09:32'))
print(timestamp_to_timestamp10(1506816572546))
print(date_style_transfomation('2017-10-01 08:09:32'))

结果为

1506836224000
2017-10-01 13:37:04
2017-10-01 08:09:32
1506816572
1506816572
2017-10-01 08-09-32

以上这篇Python之time模块的时间戳,时间字符串格式化与转换方法(13位时间戳)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现在无须过多援引的情况下创建字典的方法
Sep 25 Python
使用Python抓取模板之家的CSS模板
Mar 16 Python
图文讲解选择排序算法的原理及在Python中的实现
May 04 Python
TensorFlow模型保存/载入的两种方法
Mar 08 Python
python实现对文件中图片生成带标签的txt文件方法
Apr 27 Python
python单例模式实例解析
Aug 28 Python
Python输出\u编码将其转换成中文的实例
Dec 15 Python
Python正则表达式和re库知识点总结
Feb 11 Python
Python如何调用外部系统命令
Aug 07 Python
Python 批量读取文件中指定字符的实现
Mar 06 Python
python中sort sorted reverse reversed函数的区别说明
May 11 Python
写好Python代码的几条重要技巧
May 21 Python
浅谈Python2之汉字编码为unicode的问题(即类似\xc3\xa4)
Aug 12 #Python
基于Python2、Python3中reload()的不同用法介绍
Aug 12 #Python
Python递归函数 二分查找算法实现解析
Aug 12 #Python
基于Python安装pyecharts所遇的问题及解决方法
Aug 12 #Python
Django实现发送邮件找回密码功能
Aug 12 #Python
使用pyecharts生成Echarts网页的实例
Aug 12 #Python
10分钟教你用python动画演示深度优先算法搜寻逃出迷宫的路径
Aug 12 #Python
You might like
一个php生成16位随机数的代码(两种方法)
2014/09/16 PHP
php修改文件上传限制方法汇总
2015/04/07 PHP
Zend Framework教程之MVC框架的Controller用法分析
2016/03/07 PHP
做网页的一些技巧(续)
2007/02/01 Javascript
把textarea中字符串里含有的回车换行替换成<br>的javascript代码
2007/04/20 Javascript
读jQuery之七 判断点击了鼠标哪个键的代码
2011/06/21 Javascript
jQuery Tools tab使用介绍
2012/07/14 Javascript
如何获取select下拉框的值(option没有及有value属性)
2013/11/08 Javascript
浅谈javascript中的instanceof和typeof
2015/02/27 Javascript
js实现超酷的照片墙展示效果图附源码下载
2015/10/08 Javascript
基于JavaScript实现网页倒计时自动跳转代码
2015/12/28 Javascript
JavaScript驾驭网页-获取网页元素
2016/03/24 Javascript
jQuery设置和获取select、checkbox、radio的选中值方法
2017/01/01 Javascript
从源码看angular/material2 中 dialog模块的实现方法
2017/10/18 Javascript
Vue用v-for给src属性赋值的方法
2018/03/03 Javascript
JS实现调用本地摄像头功能示例
2018/05/18 Javascript
JS this关键字在ajax中使用出现问题解决方案
2020/07/17 Javascript
vue+elementUI 实现内容区域高度自适应的示例
2020/09/26 Javascript
js中复选框的取值及赋值示例详解
2020/10/18 Javascript
vue element和nuxt的使用技巧分享
2021/01/14 Vue.js
PyTorch学习笔记之回归实战
2018/05/28 Python
Python实现多级目录压缩与解压文件的方法
2018/09/01 Python
详解Python locals()的陷阱
2019/03/26 Python
python3实现的zip格式压缩文件夹操作示例
2019/08/17 Python
对tensorflow中的strides参数使用详解
2020/01/04 Python
pycharm 2018 激活码及破解补丁激活方式
2020/09/21 Python
Python中的__init__作用是什么
2020/06/09 Python
英国内衣连锁店:Boux Avenue
2018/01/24 全球购物
捐款倡议书范文
2014/02/02 职场文书
营业员岗位职责范本
2015/04/14 职场文书
民事辩护词范文
2015/05/21 职场文书
婚礼上证婚人致辞
2015/07/28 职场文书
公司董事任命书
2015/09/21 职场文书
导游词之太行山青龙峡
2020/01/14 职场文书
选购到合适的激光打印机
2022/04/21 数码科技
Ubuntu安装Mysql+启用远程连接的完整过程
2022/06/21 Servers