Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 638
  • Last Modified:

how to coonvert date in python

I have date in this format dd Month yyyy and I need to convert in dd/mm/yyyy in python.

e.g. 07 November 2006 to 07/11/2006


Any ideas? thanks
0
mmalik15
Asked:
mmalik15
  • 3
1 Solution
 
HonorGodCommented:
Use the datetime module:

>>> import datetime
>>>
>>> when = datetime.date( 2006, 11, 7 )
>>> when.strftime( '%d/%m/%Y' )
'07/11/2006'
>>>

Open in new window

0
 
mmalik15Author Commented:
sorry my dates are in the following formats

20 December 2011

18 October 2010

28 July 2008

etc and I need

20/12/20111
18/10/2010
28/07/2008


thanks
0
 
HonorGodCommented:
Ah, so you want to parse the input date (e.g., "20 December 2011"), and format it..

How about something like this:

>>> import datetime
>>>
>>> dates = '20 December 2011,18 October 2010,28 July 2008'.split( ',' )
>>> format = '%d %B %Y'
>>>
>>> for date in dates :
...     when = datetime.datetime.strptime( date, format )
...     print '%s from %s' % ( when.strftime( '%d/%m/%Y' ), date )
...
20/12/2011 from 20 December 2011
18/10/2010 from 18 October 2010
28/07/2008 from 28 July 2008
>>>

Open in new window

0
 
HonorGodCommented:
Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now