如何验证字符串为正确的日期格式 [英] how to verified the string as correct date format
本文介绍了如何验证字符串为正确的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能知道验证字符串的最佳方法是在以下格式中吗?我不确定如何验证房屋和(上午或下午)
can I know the best way for me to verified the string is in the following formate. I am not sure how i should verified the house and (am or pm)
datestring = 'Feb 22 2017, 1:27:42pm'
print(datetime.strptime('Feb 22 2017, 1:27:42', '%b %d %Y, %H:%m:%S'))
推荐答案
您的主要问题实际上是%m
应该为%M
。小写的 m
是月份,而不是分钟。 %p
将解析'am'
和'pm'
。您可能还想将%H
更改为%I
,因为am / pm的存在意味着需要12小时制大于24:
Your main issue is actually that %m
should be %M
. Lowercase m
is the month, not the minute. %p
will parse 'am'
and 'pm'
. You may also want to change %H
to %I
since the presence of am/pm implies a 12 hour clock rather than 24:
datestring = 'Feb 22 2017, 1:27:42pm'
datetime.strptime(datestring, '%b %d %Y, %I:%M:%S%p')
结果在 datetime
对象中,该对象可以用我的语言环境/平台表示为 2017-02-22 13:27:42
默认值。
Results in a datetime
object that can be represented as 2017-02-22 13:27:42
with my locale/platform defaults.
这篇关于如何验证字符串为正确的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文