Home  >  Q&A  >  body text

python - dateutil.parser handles incomplete time strings

parse function is powerful and convenient, and it is easy to handle time analysis in various string forms. However, when encountering an incomplete time format, such as '8-9 12:00', it can also be automatically parsed into 2017-8- 9 12:00, the default current year is used to complete the missing content.

The issue is

Is there a way to directly obtain the parsed string without automatically completing the missing parts?

Sorry, the question is not detailed enough, please add more.
For example "Juanary 1, 12:00"
Parse to get <datetime> 2017-1-1 12:00:00
All I need is <str> 1-1 12:00

In other words, I hope to block the automatic completion function and only need the parsed string of the actual content. Is there any good way to achieve this?

I checked the official documentation and found that there is no corresponding function. There is just a time template that is set to be replaced by default.

过去多啦不再A梦过去多啦不再A梦2686 days ago777

reply all(2)I'll reply

  • 阿神

    阿神2017-06-12 09:29:04

    Can’t we use datetime’s strftime to format it?

    print parse('2017-1-1 12:00:00').strftime('%m-%d %H:%M')

    reply
    0
  • 学习ing

    学习ing2017-06-12 09:29:04

    It is indeed possible, but I have no way to determine whether the obtained data has been completed and which parts have been completed.
    That’s the trouble.
    Is there any way to deal with it?

    reply
    0
  • Cancelreply