python 日期时间帮助

发布于 2024-09-14 12:56:01 字数 706 浏览 3 评论 0原文

我有一个很大的搜索字段,用户可以在其中输入任何文本,它就会搜索它。

现在我需要将 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 技术交流群。

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

发布评论

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

评论(2

红墙和绿瓦 2024-09-21 12:56:01

Python-dateutil 应该会让你的生活更轻松。

from dateutil.parser import parse as dparse
for each in ('25/08/1970', '25-08-1970', '1970/08/25', '1970-08-25'):
    dparse(each)

dparse(each) 将返回 datetime .datetime 实例。您可以从 datetime 实例中获取日期、月份和年份。

更新

正如 mp0int 指出的,请记住本地化。

Python-dateutil should make your life much easier.

from dateutil.parser import parse as dparse
for each in ('25/08/1970', '25-08-1970', '1970/08/25', '1970-08-25'):
    dparse(each)

dparse(each) will return a datetime.datetime instance. You can pick up the date, month and year from the datetime instance.

Update

As mp0int pointed out, do remember to localize.

久夏青 2024-09-21 12:56:01

Dateutil.parser.parse是一个令人讨厌的函数,必须小心使用,例如

In [16]: parse('2010-05-01')
Out[16]: datetime.datetime(2010, 5, 1, 0, 0)

In [17]: parse('01-05-2010')
Out[17]: datetime.datetime(2010, 1, 5, 0, 0)

本地化在日期时间格式中是一个重要的事情。

a = parse('01-05-2010')
a.astimezone(dateutil.tx.tzutc()) # not sure about dateutil.tx.tzutc()

也许这会解决您的问题,但我还没有使用它,并且我不确定女巫 dateutil.tx 函数是否是您所需要的。

Dateutil.parser.parse is a nasty function and must be used carefully, such as

In [16]: parse('2010-05-01')
Out[16]: datetime.datetime(2010, 5, 1, 0, 0)

In [17]: parse('01-05-2010')
Out[17]: datetime.datetime(2010, 1, 5, 0, 0)

Localization is an important matter in date time format.

a = parse('01-05-2010')
a.astimezone(dateutil.tx.tzutc()) # not sure about dateutil.tx.tzutc()

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.

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