1

I would like to convert strings to datetime objects to be used in the insert statement for MySQL. The strings are received in the following format :

2010-12-21T22:57:04.000Z

The data type of the MySQL column is DATETIME.

2
  • As far as I know, DATETIME column type does not support milliseconds, so you would lose that bit of information. Is this ok? Commented Jul 4, 2011 at 20:42
  • Hexa, YES. I wouldn't mind losing the milli seconds. Commented Jul 4, 2011 at 20:55

1 Answer 1

4

You can use the strptime function.

For instance, that would give:

myDatetime = datetime.strptime(myString.split(".")[0], "%Y-%m-%dT%H:%M:%S")

[EDIT] Well, I've seen this has been treated in another thread with a better answer than mine: How to parse an ISO 8601-formatted date?

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

2 Comments

Thanks, The dateutil.parser function looks good. I get the following : 2010-12-21 22:57:04+00:00 Would this do for the datetime column?
@Denzil: i can't see why it wouldn't but i am often using DAL when Python programming so i am not an expert in this part. Btw, as hexa pointed out, you'll probably loose your miliseconds. You should try and see if it behaves as you wish

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.