使用Python比较不同格式的日期和时间 [英] Comparing dates and times in different formats using Python

查看:837
本文介绍了使用Python比较不同格式的日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件中有以下格式的行。

I have lines of the following format in a file.

摘要;会议;说明;无; DateStart; 20100629T110000; DateEnd; 20100629T120000;时间; 20100805T084547Z

Summary;meeting;Description;None;DateStart;20100629T110000;DateEnd;20100629T120000;Time;20100805T084547Z

需要创建一个具有两个输入的功能:时间和日期格式如下格式时间:HH:MM和日期为mmddyyyy。 (这些是字符串)。现在函数需要读取这行,看看输入日期和时间是否在DateStart(20100629T11000)和DateEnd(20100629T120000)之间。我如何处理这个,因为输入和行中日期和时间的格式有两种格式?

I need to create a function that has two inputs: time and date in the following formats Time: HH:MM and date as mmddyyyy. (These are strings). Now the function needs to read this line and see if the the input date and time, lies between DateStart(20100629T11000) and DateEnd(20100629T120000). How do i deal with this since the format of date and time in the input and the line are in two formats?

推荐答案

你可以将字符串解析为 datetime strptime

>>> datetime.datetime.strptime('20100629T110000', '%Y%m%dT%H%M%S')
datetime.datetime(2010, 6, 29, 11, 0)
>>> datetime.datetime.strptime('23:45 06192005', '%H:%M %m%d%Y')
datetime.datetime(2005, 6, 19, 23, 45)

然后你可以比较(< <= 等)两个数据时间。

And then you can compare (<, <=, etc) the two datetimes.

这篇关于使用Python比较不同格式的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆