Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6182033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:07:56+00:00 2026-05-24T01:07:56+00:00

I want to convert date from one format to another.Let’s say, I have some

  • 0

I want to convert date from one format to another.Let’s say, I have some date:

February 27, 1820
April 3, 1885

And I want to convert it:

27 February 1820
3 April 1885

I tried but getting error:

dt = "February 12, 1809" 
dt = datetime.strptime('%d %b %Y') 

Any kind of help will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:07:56+00:00Added an answer on May 24, 2026 at 1:07 am

    Yesterday, I learned from Kirk Strauser that strptime()) is much more slower than other solutions: see this file

    So my advice is to use another way. For exemple:

    import re
    
    ss = '''February 27, 1820
    a line
    April 3, 1885'''
    
    regx = re.compile('(January|February|March|'
                      'April|May|June'
                      'July|August|September|'
                      'October|November|December)'
                      ' '
                      '(\d|[012]\d|3[01])'
                      ',(?= \d{4})')
    
    print regx.findall(ss)
    print
    print regx.sub('\\2 \\1',ss)
    

    Edit 1

    The speed of the program can be improved using regx.sub(repl,ss) with repl() being a function that doesn’t extract the month and day as group(1) and group(2), but by slicing:

    import re
    from time import clock
    
    ss = '''February 27, 1820
    a line
    April 3, 1885'''
    
    regx = re.compile('(January|February|March|'
                      'April|May|June'
                      'July|August|September|'
                      'October|November|December)'
                      ' '
                      '(\d|[012]\d|3[01])'
                      ',(?= \d{4})')
    
    print regx.findall(ss)
    print
    
    
    te = clock()
    for i in xrange(10000):
        x = regx.sub('\\2 \\1',ss)
    print clock()-te
    print x
    print
    
    
    
    
    regx = re.compile('(?:January|February|March|'
                      'April|May|June'
                      'July|August|September|'
                      'October|November|December)'
                      ' '
                      '(?:\d|[012]\d|3[01]),'
                      '(?= \d{4})')
    
    def repl(mat):
        sp = mat.group().split()
        return sp[1][0:-1] + ' ' + sp[0]
    
    te = clock()
    for i in xrange(1):
        y = regx.sub(repl,ss)
    print clock()-te
    print y
    

    result

    [('February', '27'), ('April', '3')]
    
    2.52965614345
    27 February 1820
    a line
    3 April 1885
    
    0.378833622709
    27 February 1820
    a line
    3 April 1885
    

    PS: I also knew that there is a problem of span of time covered by strftime and strptime (not before 1900) , that’s why I immediately choosed to treat the problem with a regex. People find regexes too heavy and impressive to resort to them, but I don’t understand this trend, because as soon as you master just a little the regexes, you can do plenty of things, with efficiency and speed. Hura for the regex tool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Date format: 2009-08-10T16:03:03Z that I want to convert to: @MMM dd,
I want to convert date format from 01/09 to January 2009 , 09/03 to
What's the quickest way to convert a date in one format, say 2008-06-01 to
I have strings like 84, 03 etc. that I want to convert to Date
For some unfortunate reasons, I have to convert a proprietary and binary library from
I have a large table of birthdays that I want to convert from a
I'm getting date-time from a source as 20110731183330. I want to convert it to
I want to convert String to Date in different formats. For example, I am
I want to convert for example a particular date 12-11-2008 11:33:04.510 to UTC datetime.
I want to convert a string into a double and after doing some math

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.