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


Posted in Python onFebruary 06, 2020

本文实例讲述了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.3water.com/jisuanqi/date_jisuanqi

在线万年历日历:
http://tools.3water.com/bianmin/wannianli

在线阴历/阳历转换工具:
http://tools.3water.com/bianmin/yinli2yangli

Unix时间戳(timestamp)转换工具:
http://tools.3water.com/code/unixtime

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

Python 相关文章推荐
python求众数问题实例
Sep 26 Python
Python的Flask框架中使用Flask-Migrate扩展迁移数据库的教程
Jun 14 Python
Python实现求解括号匹配问题的方法
Apr 17 Python
对pycharm代码整体左移和右移缩进快捷键的介绍
Jul 16 Python
Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件封装函数的方法
Sep 23 Python
django 简单实现登录验证给你
Nov 06 Python
Python实现实时数据采集新型冠状病毒数据实例
Feb 04 Python
Python Selenium截图功能实现代码
Apr 26 Python
Flask缓存静态文件的具体方法
Aug 02 Python
20行Python代码实现一款永久免费PDF编辑工具的实现
Aug 27 Python
手把手教你配置JupyterLab 环境的实现
Feb 02 Python
python中time tzset()函数实例用法
Feb 18 Python
python通过matplotlib生成复合饼图
Feb 06 #Python
tensorflow 初始化未初始化的变量实例
Feb 06 #Python
python中count函数简单的实例讲解
Feb 06 #Python
tensorflow之变量初始化(tf.Variable)使用详解
Feb 06 #Python
使用TensorFlow搭建一个全连接神经网络教程
Feb 06 #Python
详解python 降级到3.6终极解决方案
Feb 06 #Python
PyCharm如何导入python项目的方法
Feb 06 #Python
You might like
PHP输出当前进程所有变量/常量/模块/函数/类的示例
2013/11/07 PHP
Thinkphp无限级分类代码
2015/11/11 PHP
2017年最新PHP经典面试题目汇总(上篇)
2017/03/17 PHP
PHP创建对象的六种方式实例总结
2019/06/27 PHP
Javascript读取cookie函数代码
2010/10/16 Javascript
javascript和jquery修改a标签的href属性
2013/12/16 Javascript
jQuery获取当前点击的对象元素(实现代码)
2016/05/19 Javascript
svg动画之动态描边效果
2017/02/22 Javascript
详解使用vscode+es6写nodejs服务端调试配置
2017/09/21 NodeJs
javascript中的replace函数(带注释demo)
2018/01/07 Javascript
node结合swig渲染摸板的方法
2018/04/11 Javascript
Bootstrap table中toolbar新增条件查询及refresh参数使用方法
2018/05/18 Javascript
AngularJS实现动态切换样式的方法分析
2018/06/26 Javascript
4个顶级开源JavaScript图表库
2018/09/29 Javascript
Vue.js数字输入框组件使用方法详解
2019/10/19 Javascript
[02:04]2020年夜魇暗潮预告片
2020/10/30 DOTA
sqlalchemy对象转dict的示例
2014/04/22 Python
Python 获取新浪微博的最新公共微博实例分享
2014/07/03 Python
Python检测生僻字的实现方法
2016/10/23 Python
python 上下文管理器使用方法小结
2017/10/10 Python
python编写微信远程控制电脑的程序
2018/01/05 Python
tensorflow中next_batch的具体使用
2018/02/02 Python
pandas.loc 选取指定列进行操作的实例
2018/05/18 Python
python range()函数取反序遍历sequence的方法
2018/06/25 Python
python运行时强制刷新缓冲区的方法
2019/01/14 Python
python 通过 pybind11 使用Eigen加速代码的步骤
2020/12/07 Python
外企测试工程师面试题
2015/02/01 面试题
软件测试工程师结构化面试题库
2016/11/23 面试题
人事专员岗位职责
2013/11/20 职场文书
社区七一党员活动方案
2014/01/25 职场文书
感恩教育月活动总结
2014/07/07 职场文书
2014年团支部工作总结
2014/11/17 职场文书
医德医风学习心得体会
2016/01/25 职场文书
Python中glob库实现文件名的匹配
2021/06/18 Python
Python可视化学习之matplotlib内置单颜色
2022/02/24 Python
css如何把元素固定在容器底部的四种方式
2022/06/16 HTML / CSS