python 日期时间帮助
我有一个很大的搜索字段,用户可以在其中输入任何文本,它就会搜索它。
现在我需要将 dob 添加到我的搜索中。我没有添加带有 dob 选择器的新文本框。我只是希望用户能够输入多种格式的 dob,它会计算出来。
IE 用户可以使用以下任意组合进行搜索:
- 25/08/1970
- 25-08-1970
- 1970/08/25
- 1970-08-25
我的程序必须计算出每个的 dmy。
有更好的办法吗?
def parse(dob):
for d in dob.split(" "):
# find the dob
if len(d) == 10:
# its a dob
if d[0:4].isdigit() # this is the year
year = d[0:4]
month = d[5:7]
day = d[8:10]
elif d[6:10].isdigit() # this is the year
day = d[0:2]
month = d[3:5]
year= d[6:10]
I have a big search field, where users could type in any peice of text and it would search for it.
Now I have a requirement to add in dob to my search. I am not adding a new textbox with a dob picker. I just want users to be able to input a dob in a range of formats and it would figure it out.
IE users can search using any combination of these:
- 25/08/1970
- 25-08-1970
- 1970/08/25
- 1970-08-25
My program must figure out the dmy for each.
Is there a better way?
def parse(dob):
for d in dob.split(" "):
# find the dob
if len(d) == 10:
# its a dob
if d[0:4].isdigit() # this is the year
year = d[0:4]
month = d[5:7]
day = d[8:10]
elif d[6:10].isdigit() # this is the year
day = d[0:2]
month = d[3:5]
year= d[6:10]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Python-dateutil 应该会让你的生活更轻松。
dparse(each)
将返回datetime .datetime
实例。您可以从datetime
实例中获取日期、月份和年份。更新
正如 mp0int 指出的,请记住本地化。
Python-dateutil should make your life much easier.
dparse(each)
will return adatetime.datetime
instance. You can pick up the date, month and year from thedatetime
instance.Update
As mp0int pointed out, do remember to localize.
Dateutil.parser.parse是一个令人讨厌的函数,必须小心使用,例如
本地化在日期时间格式中是一个重要的事情。
也许这会解决您的问题,但我还没有使用它,并且我不确定女巫 dateutil.tx 函数是否是您所需要的。
Dateutil.parser.parse is a nasty function and must be used carefully, such as
Localization is an important matter in date time format.
probably this will resolve your problem, but i have not use it and i am not sure witch dateutil.tx function is what you need.