2

I am new to python and i have written a script that converts a string date coming in to a datetime format going out. My problem is that cannot convert the datetime object back to a string for manipulation. i have a date eg 2011-08-10 14:50:10 all i need to to is add a T between the date and time and a Z at the end. unfortunately im using python 2.3 as my application will only accept that.

my code is as follows:

fromValue= ''
fromValue = document.Get(self._generic3)
fromValue = fromValue[:fromValue.rindex(" ")]
fromValue = datetime.datetime.fromtimestamp(time.mktime(time.strptime(fromValue,"%a, %d %b %Y %H:%M:%S")))
1
  • 3
    Hi Clayton, welcome to Stack Overflow. I suggest you post a "working" example (e.g. replace the call to document.Get with its return value). Commented Aug 11, 2011 at 9:38

3 Answers 3

8
toValue = fromValue.strftime("%Y-%m-%dT %H:%M:%SZ")

This should work fine. datetime.strftime was available on Python 2.3.

You'd certainly be better off upgrading to at least Python 2.5 if at all possible. Python 2.3 hasn't even received security patches in years.

Edit: Also, you don't need to initialize or declare variables in Python; the fromValue= '' has no effect on your program.

Edit 2: In a comment, you seem to have said you have it in a string already in nearly the right format:

"2011-08-08 14:15:21"

so just do

'T'.join("2011-08-08 14:15:21".split()) + 'Z'

If you want to add the letters while it's a string.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks guys for the help you really helped me alot. @unubtu your answered worked all i had to do was specify the isoformat which i did and worked!!! thanks a mill
3

It looks like you are trying to format the datetime in ISO-8601 format. For this purpose, use the isoformat method.

import datetime as dt 
try:
    import email.utils as eu
except ImportError:
    import email.Utils as eu  # for Python 2.3

date_string="Fri, 08 Aug 2011 14:15:10 -0400"
ttuple=eu.parsedate(date_string)
date=dt.datetime(*ttuple[:6])
print(date.isoformat()+'Z')

yields

2011-08-08T14:15:10Z

Here is link to isoformat and parsedate in the Python2.3 docs.

1 Comment

Thanks for the feedback i really appreciate it @unubtu my string comes in looking like this "Fri, 08 Aug 2011 14:15:10 -0400" i have chopped of the "-0400" and kept the rest of the string which i have converted to this format "2011-08-08 14:15:21" i need from here that converted into this '2011-08-08T14:50:10Z' which i cannot do i get an error saying integer required when i try to split it
3
fromValue.strftime('%Y-%m-%d T %H:%M:%S Z')

http://docs.python.org/release/2.3/lib/module-time.html

http://docs.python.org/release/2.3/lib/node208.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.