python时间日期操作方法实例小结

所属分类: 脚本专栏 / python 阅读数: 1796
收藏 0 赞 0 分享

本文实例讲述了python时间日期操作方法。分享给大家供大家参考,具体如下:

#coding=utf-8
import time
import datetime
if __name__ == "__main__":
 # 今天
 now = datetime.datetime.now()
 print now.strftime('%Y-%m-%d %H:%M:%S')
 print "%s-%s-%s %s:%s:%s" % (now.year, now.month, now.day, now.hour, now.minute, now.second)
 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
 # 前一天
 now = datetime.datetime.now()
 dt = now + datetime.timedelta(days=-1)
 print dt.strftime('%Y-%m-%d %H:%M:%S')
 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() - 24 * 3600))
 # 后一天
 now = datetime.datetime.now()
 dt = now + datetime.timedelta(days=1)
 print dt.strftime('%Y-%m-%d %H:%M:%S')
 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 24 * 3600))
 # 前一小时
 now = datetime.datetime.now()
 dt = now - datetime.timedelta(hours=1)
 print dt.strftime("%Y-%m-%d %H:%M:%S")
 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() - 1 * 3600))
 # 时间戳 秒
 print int(time.time())
 # 时间戳 毫秒
 print int(round(time.time() * 1000))
 # 时间戳 to 日期
 print datetime.datetime.fromtimestamp(1507630854)
 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1507630854))
 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
 # 日期 to 时间戳
 print time.mktime(time.strptime("2017-10-10", "%Y-%m-%d"))
 print time.mktime(time.strptime("2017-10-10 10:10:10", "%Y-%m-%d %H:%M:%S"))

运行结果:

2020-02-06 11:33:51
2020-2-6 11:33:51
2020-02-06 11:33:51
2020-02-05 11:33:51
2020-02-05 11:33:51
2020-02-07 11:33:51
2020-02-07 11:33:51
2020-02-06 10:33:51
2020-02-06 10:33:51
1580960031
1580960031893
2017-10-10 18:20:54
2017-10-10 18:20:54
2020-02-06 11:33:51
1507564800.0
1507601410.0

PS:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:

在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在线万年历日历:
http://tools.jb51.net/bianmin/wannianli

在线阴历/阳历转换工具:
http://tools.jb51.net/bianmin/yinli2yangli

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python日期与时间操作技巧总结》、《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

更多精彩内容其他人还在看

Python实现图像几何变换

这篇文章主要介绍了Python实现图像几何变换的方法,实例分析了Python基于Image模块实现图像翻转、旋转、改变大小等操作的相关技巧,非常简单实用,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中的urllib模块使用详解

这篇文章主要介绍了Python中的urllib模块使用详解,是Python入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

Python的多态性实例分析

这篇文章主要介绍了Python的多态性,以实例形式深入浅出的分析了Python在面向对象编程中多态性的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

python生成IP段的方法

这篇文章主要介绍了python生成IP段的方法,涉及Python文件读写及随机数操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python操作redis的方法

这篇文章主要介绍了python操作redis的方法,包括Python针对redis的连接、设置、获取、删除等常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python妹子图简单爬虫实例

这篇文章主要介绍了python妹子图简单爬虫,实例分析了Python爬虫程序所涉及的页面源码获取、进度显示、正则匹配等技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

分析用Python脚本关闭文件操作的机制

这篇文章主要介绍了分析用Python脚本关闭文件操作的机制,作者分Python2.x版本和3.x版本两种情况进行了阐述,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现搜索指定目录下文件及文件内搜索指定关键词的方法

这篇文章主要介绍了python实现搜索指定目录下文件及文件内搜索指定关键词的方法,可实现针对文件夹及文件内关键词的搜索功能,需要的朋友可以参考下
收藏 0 赞 0 分享

python中getaddrinfo()基本用法实例分析

这篇文章主要介绍了python中getaddrinfo()基本用法,实例分析了Python中使用getaddrinfo方法进行IP地址解析的基本技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python查找指定具有相同内容文件的方法

这篇文章主要介绍了python查找指定具有相同内容文件的方法,涉及Python针对文件操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多