how to coonvert date in python

Posted on 2012-09-13
Last Modified: 2012-09-13
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
Question by:mmalik15
    LVL 41

    Expert Comment

    Use the datetime module:

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

    Open in new window


    Author Comment

    sorry my dates are in the following formats

    20 December 2011

    18 October 2010

    28 July 2008

    etc and I need


    LVL 41

    Accepted Solution

    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

    LVL 41

    Expert Comment

    Thanks for the grade & points.

    Good luck & have a great day.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now