By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,068 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

How to convert string to date object?

P: 6
i have date in string in this format
eg. 1/20/21011 i need to convert it into the date object
Jan 25 '11 #1

✓ answered by bvdet

Expand|Select|Wrap|Line Numbers
  1. >>> import time
  2. >>> s = "1/20/2011"
  3. >>> time.strptime(s, "%m/%d/%Y")
  4. (2011, 1, 20, 0, 0, 0, 3, 20, -1)
  5. >>> 

Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. >>> import time
  2. >>> s = "1/20/2011"
  3. >>> time.strptime(s, "%m/%d/%Y")
  4. (2011, 1, 20, 0, 0, 0, 3, 20, -1)
  5. >>> 
Jan 25 '11 #2

P: 6
thanks i tried it and it works fine i alos tried same for time also but i get problem when the time is provide in this format
10:30:00 AM
Jan 26 '11 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. >>> import time
  2. >>> s = "1/20/2011 10:30:00 AM"
  3. >>> time.strptime(s, "%m/%d/%Y %I:%M:%S %p")
  4. (2011, 1, 20, 10, 30, 0, 3, 20, -1)
  5. >>> time.strptime("1/20/2011 10:30:00 PM", "%m/%d/%Y %I:%M:%S %p")
  6. (2011, 1, 20, 22, 30, 0, 3, 20, -1)
  7. >>> 
Jan 26 '11 #4

P: 6
Thanks very much
Jan 26 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.