Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting datetime format from String?

In Python there are multiple DateTime parser's which can parse a date string automatically without proving the datetime format. e.g. DateParser, DateUtils. However, There is no option in any of them where the format in which the date was parsed is returned. What should be done to get the format with which automatic parser's parsed the date as in the example below? e.g parse("2019/05/20") -- > [datetime(2019,5,20,0,0) , "%Y/%m/%d"]


1 Answers

If you want the format string to be able to format datetime objects the same way, I don’t think this is possible, because these parsers (at least dateparser) can parse dates that simply cannot be expressed as a format string.

There is a feature request for it, though. Maybe it can be implemented in some way, or for a subset of all possible inputs.

If you want this for debugging purposes, to understand how a given string could cause a given datetime, I don’t think there is an easy way at the moment either. But if you are getting unexpected results, there are settings you can use to influence different parsing aspects to get the desired results. Date order, input format strings and locales can make quite a difference.

like image 180
Gallaecio Avatar answered Aug 11 '26 14:08

Gallaecio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!