954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Parsing a date into MM/DD/YYYY HH:MM:SS format

Hello,

I got my last issue resolved, working great thanks to you guys. I have a wide variety of date formats that I need to parse into this format: MM/DD/YYYY HH:MM:SS (a mysql DATETIME type value)..

I have tried:

post_date = time.strftime("%Y-%m%d %H:%M:%S", i.issued)


However, this gives me:

Traceback (most recent call last):
  File "./parse.py", line 34, in ?
    print time.strftime("%Y-%m-%d %H:%M:%S", i.issued)
TypeError: argument must be sequence of length 9, not 20


i.issued is a variable, with this as an (example) value:
(2005, 7, 24, 15, 32, 0, 6, 205, 0). I believe this is called a tuple?

If anybody knows how I can successfully parse this type of value into the MM/DD/YYYY HH:MM:SS format, I would be extremely grateful. Thank you! :)

mattd2
Newbie Poster
5 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

This works fine ...

import time

timeTuple = (2005, 7, 24, 15, 32, 0, 6, 205, 0)
print "MM/DD/YYYY HH:MM:SS =",time.strftime("%m/%d/%y %H:%M:%S", timeTuple)

There must be something wrong with i.issued, you can force it to be a tuple with tuple(i.issued).

You can create this timeTuple with ...

timeTuple = time.strptime("07/24/05 15:32:00", "%m/%d/%y %H:%M:%S")
print timeTuple   # shows (2005, 7, 24, 15, 32, 0, 6, 205, -1)

The -1 is just the daylight savings time flag.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You