Converting DD MM YYYY into YYYY-MM-DD?

Rami Chowdhury rami.chowdhury at gmail.com
Mon Aug 17 20:18:33 EDT 2009


Correct me if I'm wrong, but doesn't  
http://docs.python.org/library/datetime.html#datetime.datetime.strptime do  
this?

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'FR')
   'French_France.1252'
>>> date_str = '05 Mai 2009 - 18h25'
>>> fmt = '%d %B %Y - %Hh%M'
>>> date_obj = datetime.strptime(date_str, fmt)
>>> date_obj
   datetime.datetime(2009, 5, 5, 18, 25)
>>> date_obj.strftime('%Y-%m-%d %H:%M')
   '2009-05-05 18:25'

If you're using a recent enough version of Python (2.5 and up) I'd imagine  
that's the best way to do it?

On Mon, 17 Aug 2009 16:58:28 -0700, Che M <cmpython at gmail.com> wrote:

> On Aug 17, 6:26 pm, Gilles Ganault <nos... at nospam.com> wrote:
>> Hello,
>>
>>         I need to convert DD MM YYYY dates into the MySQL-friendly
>> YYYY-MM-DD, and translate the month name from literal French to its
>> numeric equivalent (eg. "Janvier" into "01").
>>
>> Here's an example:
>>
>> SELECT dateinscription, dateconnexion FROM membres LIMIT 1;
>> 26 Mai 2007|17 Août 2009 - 09h20
>>
>> I'd like to update the row into "2007-05-26" and "2009-08-17 09:20",
>> respectively.
>>
>> What is the best way to do this in Python?
>>
>> Thank you.
>
> Likely this is not the best way, but I would do, for
> the first one (and the same idea for the second):
>
> def convert(date):
>     frenchdict = {'Mai':'May'} #etc...
>     day = mystring[:2]
>     month = frenchdict[ mystring[3:6] ]
>     year = mystring[7:11]
>     newdate = year+'-'+month+'-'+day
>     print 'newdate is ', newdate
>



-- 
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)



More information about the Python-list mailing list