Python时间日期操作常用方法汇总

江岸
江岸
发布时间:2026-10-13| 6人浏览过
原创

本文系统总结了Python中处理时间与日期的核心方法,涵盖使用datetime和time模块进行当前时间获取、日期加减运算(天/小时)、字符串与时间戳互转等常见场景。通过具体代码示例与运行结果,帮助开发者快速掌握Python时间处理技巧,适用于日志分析、定时任务及数据清洗等实际开发需求。

Python时间日期操作概述

本文详细介绍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

推荐在线辅助工具

此外,推荐以下在线工具辅助日期与天数计算:

在线日期/天数计算器: 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 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。