博客
关于我
datetime.timedelta类型转换为int类型
阅读量:726 次
发布时间:2019-03-22

本文共 377 字,大约阅读时间需要 1 分钟。

设置日期并计算时差

在Python中,我们可以通过datetime库轻松设置日期并计算两个日期之间的时差。以下是详细的实现步骤:

import datetimeprev_day = datetime.datetime(2017, 12, 31)cur_day = datetime.datetime(2018, 1, 31)diff = cur_day - prev_day

运行此代码,可以看到diff的具体结果:

31 days, 0:00:00

如果要将diff结果转换为整数形式,可以使用diff.days属性:

print(diff.days)print(type(diff.days))

输出结果如下:

31

通过上述方法,我们可以清楚地得到两个日期之间的时差,并以整数形式存储结果。这一方法简单且高效,是处理日期计算的常用选择。

转载地址:http://dvggz.baihongyu.com/

你可能感兴趣的文章
poj 2965 The Pilots Brothers' refrigerator-1
查看>>
poj 3026( Borg Maze BFS + Prim)
查看>>
POJ 3041 - 最大二分匹配
查看>>
POJ 3041 Asteroids(二分匹配模板题)
查看>>
Qt笔记——标准文件对话框QFileDialog
查看>>
poj 3083 Children of the Candy Corn
查看>>
POJ 3083 Children of the Candy Corn 解题报告
查看>>
POJ 3253 Fence Repair C++ STL multiset 可解 (同51nod 1117 聪明的木匠)
查看>>
Qt笔记——控件总结
查看>>
poj 3262 Protecting the Flowers 贪心
查看>>
poj 3264(简单线段树)
查看>>
Qt笔记——布局管理三件套分割窗口、停靠窗口和堆栈窗口
查看>>
poj 3277 线段树
查看>>
POJ 3349 Snowflake Snow Snowflakes
查看>>
POJ 3411 DFS
查看>>
poj 3422 Kaka's Matrix Travels (费用流 + 拆点)
查看>>
Qt笔记——官方文档全局定义(二)Functions函数
查看>>
POJ 3468 A Simple Problem with Integers
查看>>
poj 3468 A Simple Problem with Integers 降维线段树
查看>>
poj 3468 A Simple Problem with Integers(线段树 插线问线)
查看>>