Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

converting string to a date format

14 views
Skip to first unread message

tekion

unread,
Dec 20, 2009, 9:23:39 PM12/20/09
to
All,
I know there is a datetime module for converting and manipulate date
format. I have this string date format: 24/Nov/2009:10:39:03 -0500
and would like to convert it to a date format of "2009-11-24
10:39:03". At the moment I am reading datetime module trying to find
out if I could do it with datetime module. Does any one know of a way
besides slashing my way through it using string split function? Thanks.

MRAB

unread,
Dec 20, 2009, 9:46:55 PM12/20/09
to pytho...@python.org

Use datetime.datetime.strptime() to parse the string; you'll need to
remove the timezone from the string first. The format you need to
provide is the same one you'd use if you were creating the string from a
datetime. Then call the .strftime() method of the resulting datetime
object, providing the appropriate format to create the new string.

Ben Finney

unread,
Dec 20, 2009, 9:54:53 PM12/20/09
to
tekion <tek...@gmail.com> writes:

> I have this string date format: 24/Nov/2009:10:39:03 -0500 and would
> like to convert it to a date format of "2009-11-24 10:39:03".

This should, ideally, consist of two separate operations:

* parse the string, using a specific format, to create a ‘datetime’
object

* create a string representation of the datetime using your preferred
string format

> At the moment I am reading datetime module trying to find out if I
> could do it with datetime module.

Unfortunately, the manipulation of time data has historically been a bit
messy in the Python standard library; the functionality for round-trip
conversion of strings and datetime values involves both the ‘datetime’
and ‘time’ modules.

If you have Python 2.6 or greater, you can perform the above steps using
<URL:http://docs.python.org/library/datetime.html#datetime.datetime.strptime>
to parse the string to a ‘datetime’ object, and get a string from the
<URL:http://docs.python.org/library/datetime.html#datetime.datetime.strftime>
method of that object.

If you have an earlier Python, you don't have ‘datetime.strptime’. So
you'll need to follow the hack suggested in the documentation above for
that method, ‘datetime(*(time.strptime(date_string, format)[0:6]))’.

--
\ “Because of the impropriety of entertaining guests of the |
`\ opposite sex in the bedroom, it is suggested that the lobby be |
_o__) used for this purpose.” —hotel, Zurich |
Ben Finney

tekion

unread,
Dec 21, 2009, 2:37:14 PM12/21/09
to
Ben,
I do not have python 2.6 install, my version of Python is 2.4.
Because of my version of Python I believe I have to perform what you
have suggested:

This should, ideally, consist of two separate operations:

* parse the string, using a specific format, to create a ‘datetime’
object

* create a string representation of the datetime using your
preferred
string format

So I guess I am stuck on parsing the string "24/Nov/2009:12:00:00
-0500" using regex and or string function to get the output to
"2009-11-24 12:00:00". It looks like I may have to use regex to
accomplish this and also re-map Nov to "11". Does any one have any
idea that would take "24/Nov/2009:HH:MM:SS" and format it to
"2009-11-24 HH:MM:SS"? Thanks

MRAB

unread,
Dec 21, 2009, 3:05:52 PM12/21/09
to pytho...@python.org
tekion wrote:
> Ben,
> I do not have python 2.6 install, my version of Python is 2.4.
> Because of my version of Python I believe I have to perform what you
> have suggested:
>
> This should, ideally, consist of two separate operations:
>
> * parse the string, using a specific format, to create a �datetime�

> object
>
> * create a string representation of the datetime using your
> preferred
> string format
>
> So I guess I am stuck on parsing the string "24/Nov/2009:12:00:00
> -0500" using regex and or string function to get the output to
> "2009-11-24 12:00:00". It looks like I may have to use regex to
> accomplish this and also re-map Nov to "11". Does any one have any
> idea that would take "24/Nov/2009:HH:MM:SS" and format it to
> "2009-11-24 HH:MM:SS"? Thanks

If you don't have the 'datetime' module then you can use the 'time'
instead. Use time.strptime() to parse the string and time.strftime() to
create the new string.

tekion

unread,
Dec 21, 2009, 5:03:10 PM12/21/09
to
On Dec 21, 3:05 pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
> tekionwrote:

> > Ben,
> > I do not have python 2.6 install, my version of Python is 2.4.
> > Because of my version of Python I believe I have to perform what you
> > have suggested:
>
> > This should, ideally, consist of two separate operations:
>
> >   * parse the string, using a specific format, to create a ‘datetime’

> >     object
>
> >   * create a string representation of the datetime using your
> > preferred
> >     string format
>
> > So I guess I am stuck on parsing the string "24/Nov/2009:12:00:00
> > -0500" using regex and or string function to get the output to
> > "2009-11-24 12:00:00".  It looks like I may have to use regex to
> > accomplish this and also re-map Nov to "11".  Does any one have any
> > idea that would take "24/Nov/2009:HH:MM:SS" and format it to
> > "2009-11-24 HH:MM:SS"? Thanks
>
> If you don't have the 'datetime' module then you can use the 'time'
> instead. Use time.strptime() to parse the string and time.strftime() to
> create the new string.

Thanks. This is what I have:

time.strftime("%Y-%m-%d %H:%M:%S", time.strptime("24/Nov/
2009:12:00:00", "%d/%b/%Y:%H:%M:%S")

and it seems to work.

Ben Finney

unread,
Dec 21, 2009, 5:05:16 PM12/21/09
to
tekion <tek...@gmail.com> writes:

> Ben,
> I do not have python 2.6 install, my version of Python is 2.4.

Ouch :-( Upgrade as soon as possible, 2.4 is no longer receiving bug
fixes <URL:http://www.python.org/download/releases/2.4.6/>.

> So I guess I am stuck on parsing the string "24/Nov/2009:12:00:00
> -0500" using regex and or string function

No, as I suggested, you can use the ‘datetime.datetime’ constructor with
the ‘time.strptime’ output. You only have available the formatting in
<URL:http://docs.python.org/library/time.html#time.strftime>, so the
numeric time zone will be un-parseable by ‘time.strptime’::

>>> import datetime
>>> import time
>>> in_text = "24/Nov/2009:12:00:00 -0500"
>>> in_time_format = "%d/%b/%Y:%H:%M:%S %Z"
>>> time.strptime(in_text, in_time_format)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/_strptime.py", line 293, in strptime
raise ValueError("time data did not match format: data=%s fmt=%s" %
ValueError: time data did not match format: data=24/Nov/2009:12:00:00 -0500 fmt=%d/%b/%Y:%H:%M:%S %Z

Instead you'll need to do as has already been suggested: strip the
numeric time zone and parse the remaining data::

>>> in_text = "24/Nov/2009:12:00:00 -0500".split(' ', 1)[0]
>>> in_text
'24/Nov/2009:12:00:00'
>>> in_time_format = "%d/%b/%Y:%H:%M:%S"
>>> time.strptime(in_text, in_time_format)
(2009, 11, 24, 12, 0, 0, 1, 328, -1)

and use the hack documented in Python 2.6's ‘datetime.datetime.strptime’
function to create a ‘datetime’ object::

>>> in_time = datetime.datetime(*(time.strptime(in_text, in_time_format)[0:6]))
>>> in_time
datetime.datetime(2009, 11, 24, 12, 0)

The benefit of upgrading to Python 2.6 is that you don't need to go
through these contortions with the ‘time’ type's output, you can use the
new ‘datetime.datetime.strptime’ method to get there in one step
<URL:http://docs.python.org/library/datetime.html#datetime.datetime.strptime>.

> to get the output to "2009-11-24 12:00:00".

Once you have a ‘datetime’ object, you can use its ‘strftime’ function
<URL:http://docs.python.org/library/datetime.html#strftime-behavior> to
create a string representation::

>>> out_time_format = "%Y-%m-%d %H:%M:%S"
>>> in_time.strftime(out_time_format)
'2009-11-24 12:00:00'

--
\ “I find the whole business of religion profoundly interesting. |
`\ But it does mystify me that otherwise intelligent people take |
_o__) it seriously.” —Douglas Adams |
Ben Finney

M.-A. Lemburg

unread,
Dec 22, 2009, 3:38:25 AM12/22/09
to tekion, pytho...@python.org

Try mxDateTime's string parser:

http://www.egenix.com/products/python/mxBase/mxDateTime/

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Dec 22 2009)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/

0 new messages