python:两个时间日期字符串的差异

发布于 2024-12-07 10:11:02 字数 159 浏览 0 评论 0 原文

我有两个日期字符串(取自用户输入并且可能相差很大)

s1 = '2011:10:01:10:30:00'
s2 = '2011:10:01:11:15:00'

我希望找到两者之间的差异(分钟)。

我应该如何解决这个问题?

I have two date strings (taken from user input and can vary greatly)

s1 = '2011:10:01:10:30:00'
s2 = '2011:10:01:11:15:00'

I wish to find the difference between the two as minutes.

How should I proceed to tackle this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷夜 2024-12-14 10:11:02
import datetime

d1 = datetime.datetime.strptime('2011:10:01:10:30:00', '%Y:%m:%d:%H:%M:%S')
d2 = datetime.datetime.strptime('2011:10:01:11:15:00', '%Y:%m:%d:%H:%M:%S')
diff = (d2 - d1).total_seconds() / 60

如果您需要处理任意日期时间格式,我不相信内置的日期时间库会为您做到这一点。也许看看类似的东西:

http://www.egenix.com/products/python/ mxBase/mxDateTime/

import datetime

d1 = datetime.datetime.strptime('2011:10:01:10:30:00', '%Y:%m:%d:%H:%M:%S')
d2 = datetime.datetime.strptime('2011:10:01:11:15:00', '%Y:%m:%d:%H:%M:%S')
diff = (d2 - d1).total_seconds() / 60

If you need to handle arbitrary datetime formats, I don't believe the built in datetime library will do that for you. Perhaps check out something like:

http://www.egenix.com/products/python/mxBase/mxDateTime/

日久见人心 2024-12-14 10:11:02

使用 datetime 模块,解析为 datetime 对象使用 strptime,然后减去。您将得到一个 timedelta。然后使用 timedelta.total_seconds() 并除以 60。

Using the datetime module, parse into a datetime object using strptime, then subtract. You'll get a timedelta. Then use timedelta.total_seconds() and divide by 60.

×纯※雪 2024-12-14 10:11:02

使用 datetime 解析字符串并转换为基本纪元时间。算一下。转换回来:

>>> from datetime import datetime
>>> s1 = '2011:10:01:10:30:00'
>>> s2 = '2011:10:01:11:15:00'
>>> d1=datetime.strptime(s1,'%Y:%m:%d:%I:%M:%S')
>>> d2=datetime.strptime(s2,'%Y:%m:%d:%I:%M:%S')
>>> d2-d1
datetime.timedelta(0, 2700)
>>> (d2-d1).total_seconds()/60
45.0

如果您正在寻找任意日期字符串解析,请查看 DateUtil解析函数

Use datetime to parse the string and convert into a base epoch time. Do the math. Convert back:

>>> from datetime import datetime
>>> s1 = '2011:10:01:10:30:00'
>>> s2 = '2011:10:01:11:15:00'
>>> d1=datetime.strptime(s1,'%Y:%m:%d:%I:%M:%S')
>>> d2=datetime.strptime(s2,'%Y:%m:%d:%I:%M:%S')
>>> d2-d1
datetime.timedelta(0, 2700)
>>> (d2-d1).total_seconds()/60
45.0

If you are looking for arbitrary date string parsing, check out DateUtil and the parse function.

秋日私语 2024-12-14 10:11:02

time 模块对此很有帮助。

import time

s1 = '2011:10:01:10:30:00'
s2 = '2011:10:01:11:15:00'

s1Time = time.strptime(s1, "%Y:%m:%d:%H:%M:%S")
s2Time = time.strptime(s2, "%Y:%m:%d:%H:%M:%S")

deltaInMinutes = (time.mktime(s2Time) - time.mktime(s1Time)) / 60.0
print deltaInMinutes, "minutes"

The time module can be helpful for this.

import time

s1 = '2011:10:01:10:30:00'
s2 = '2011:10:01:11:15:00'

s1Time = time.strptime(s1, "%Y:%m:%d:%H:%M:%S")
s2Time = time.strptime(s2, "%Y:%m:%d:%H:%M:%S")

deltaInMinutes = (time.mktime(s2Time) - time.mktime(s1Time)) / 60.0
print deltaInMinutes, "minutes"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文