364,111 Members | 2011 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

How To Convert String To Date?

Aobaidat79
P: 2
Dear Freinds
I have string contains the date like "21Jun2000"
What I want to do is to convert that string into date in the following format
21 Jun 2000

Plesae help me as soon as possible
'Thank you very much
Feb 17 '07 #1
Share this Question
Share on Google+
4 Replies


bartonc
Expert 5K+
P: 5,734
Dear Freinds
I have string contains the date like "21Jun2000"
What I want to do is to convert that string into date in the following format
21 Jun 2000

Plesae help me as soon as possible
'Thank you very much
If the format is constant you could

>>> date = "21Jun2000"
>>> day = date[:2]
>>> month = date[2:5]
>>> year = date[5:]
>>> print day, month, year
21 Jun 2000
Feb 17 '07 #2

ghostdog74
Expert 100+
P: 511
the usual way to do it for any date format string is using time.strptime and time.strftime.just an example
Expand|Select|Wrap|Line Numbers
  1. Type "help", "copyright", "credits" or "license" for more information.
  2. >>> import time
  3. >>> datestring = "21Jun2000"
  4. >>> c = time.strptime(datestring,"%d%b%Y")
  5. >>> time.strftime("%d %b %Y",c)
  6. '21 Jun 2000'
  7. >>> 
  8.  
Feb 17 '07 #3

bartonc
Expert 5K+
P: 5,734
the usual way to do it for any date format string is using time.strptime and time.strftime.just an example
Expand|Select|Wrap|Line Numbers
  1. Type "help", "copyright", "credits" or "license" for more information.
  2. >>> import time
  3. >>> datestring = "21Jun2000"
  4. >>> c = time.strptime(datestring,"%d%b%Y")
  5. >>> time.strftime("%d %b %Y",c)
  6. '21 Jun 2000'
  7. >>> 
  8.  
Hey! That's awesome. I always thought that you needed a date.date object for that to work.
Feb 17 '07 #4

Aobaidat79
P: 2
the usual way to do it for any date format string is using time.strptime and time.strftime.just an example
Expand|Select|Wrap|Line Numbers
  1. Type "help", "copyright", "credits" or "license" for more information.
  2. >>> import time
  3. >>> datestring = "21Jun2000"
  4. >>> c = time.strptime(datestring,"%d%b%Y")
  5. >>> time.strftime("%d %b %Y",c)
  6. '21 Jun 2000'
  7. >>> 
  8.  
You are a smart person
Thank you all
Feb 19 '07 #5

Post your reply

Help answer this question



Didn't find the answer to your Python question?

You can also browse similar questions: Python python datestring