strptime for different languages

Barry Scott barry at barrys-emacs.org
Tue Dec 17 13:56:22 EST 2019



> On 17 Dec 2019, at 10:37, Ulrich Goebel <ml at fam-goebel.de> wrote:
> 
> Hi,
> 
> I need to interpret a date string to get a datetime object. That should be done with strptime from the module datetime.
> 
> But I don't know enough about the locale settings from where the date sting comes. Actually the date_string cames from different contact forms inside websites made by different content management systems. So it could be

Can side step the issue?

I would change the web site to collect the date in a form that is not ambiguous.
For example use a Javascript date picker widget.

Barry


>  13. Januar 1965
>  13. January 1965
>  13.01.1965
>  1965-01-13
> or even some known other formats.
> 
> What I tried is a cascade of try's:
> 
> try:
>  d = strptime(date_string, format_string_1)
> except:
>  try:
>    d = strptime(date_string, format_string_2)
>  except:
>    try:
>      d = strptime(date_string, format_string_3)
>    except:
>      ...
> 
> That works, as long the month comes as number.
> 
> But how to cover name of months in different language?
> 
> (Shure, it would not be possible to cover all possibilities and languages. But I know the possible languages.)
> 
> Best regards
> Ulrich
> 
> -- 
> Ulrich Goebel
> Am Büchel 57, 53173 Bonn
> -- 
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list