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.

JMax
  • 26,109
  • 12
  • 69
  • 88
Dexter
  • 11,311
  • 11
  • 45
  • 61

1 Answers1

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?

Community
  • 1
  • 1
JMax
  • 26,109
  • 12
  • 69
  • 88
  • 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? – Dexter Jul 04 '11 at 21:00
  • @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 – JMax Jul 05 '11 at 06:33