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 6246323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:40:03+00:00 2026-05-24T12:40:03+00:00

I have a bunch of excel documents I am extracting dates from. I am

  • 0

I have a bunch of excel documents I am extracting dates from. I am trying to convert these to a standard format so I can put them in a database. Is there a function I can throw these strings at and get a standard format back? Here is a small sample of my data:

The good thing is I know it is always Month/Day

10/02/09
07/22/09
09-08-2008
9/9/2008
11/4/2010
 03-07-2009
09/01/2010

I’d like to get them all into MM/DD/YYYY format. Is there a way I can do this without trying each pattern against the string?

  • 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-24T12:40:05+00:00Added an answer on May 24, 2026 at 12:40 pm
    import re
    
    ss = '''10/02/09
    07/22/09
    09-08-2008
    9/9/2008
    11/4/2010
    03-07-2009
    09/01/2010'''
    
    
    regx = re.compile('[-/]')
    for xd in ss.splitlines():
        m,d,y = regx.split(xd)
        print xd,'   ','/'.join((m.zfill(2),d.zfill(2),'20'+y.zfill(2) if len(y)==2 else y))
    

    result

    10/02/09     10/02/2009
    07/22/09     07/22/2009
    09-08-2008     09/08/2008
    9/9/2008     09/09/2008
    11/4/2010     11/04/2010
    03-07-2009     03/07/2009
    09/01/2010     09/01/2010
    

    Edit 1

    And Edit 2 : taking account of the information on '{0:0>2}'.format(day) from JBernardo, I added a 4th solution, that appears to be the fastest

    import re
    from time import clock
    iterat = 100
    
    from datetime import datetime
    dates = ['10/02/09', '07/22/09', '09-08-2008', '9/9/2008', '11/4/2010',
             ' 03-07-2009', '09/01/2010']
    
    reobj = re.compile(
    r"""\s*  # optional whitespace
    (\d+)    # Month
    [-/]     # separator
    (\d+)    # Day
    [-/]     # separator
    (?:20)?  # century (optional)
    (\d+)    # years (YY)
    \s*      # optional whitespace""",
    re.VERBOSE)
    
    te = clock()
    for i in xrange(iterat):
        ndates = (reobj.sub(r"\1/\2/20\3", date) for date in dates)
        fdates1 = [datetime.strftime(datetime.strptime(date,"%m/%d/%Y"), "%m/%d/%Y")
                   for date in ndates]
    print "Tim's method   ",clock()-te,'seconds'
    
    
    
    regx = re.compile('[-/]')
    
    
    te = clock()
    for i in xrange(iterat):
        ndates = (reobj.match(date).groups() for date in dates)
        fdates2 = ['%s/%s/20%s' % tuple(x.zfill(2) for x in tu) for tu in ndates]
    print "mixing solution",clock()-te,'seconds'
    
    
    te = clock()
    for i in xrange(iterat):
        ndates = (regx.split(date.strip()) for date in dates)
        fdates3 = ['/'.join((m.zfill(2),d.zfill(2),('20'+y.zfill(2) if len(y)==2 else y)))
                  for m,d,y in ndates]
    print "eyquem's method",clock()-te,'seconds'
    
    
    
    te = clock()
    for i in xrange(iterat):
        fdates4 = ['{:0>2}/{:0>2}/20{}'.format(*reobj.match(date).groups()) for date in dates]
    print "Tim + format   ",clock()-te,'seconds'
    
    
    print fdates1==fdates2==fdates3==fdates4
    

    result

    number of iteration's turns : 100
    Tim's method    0.295053700959 seconds
    mixing solution 0.0459111423379 seconds
    eyquem's method 0.0192239516475 seconds
    Tim + format    0.0153756971906 seconds 
    True
    

    The mixing solution is interesting because it combines the speed of my solution and the ability of the regex of Tim Pietzcker to detect dates in a string.

    That’s still more true for the solution combining Tim’s one and the formating with {:0>2}. I cant’ combine {:0>2} with mine because regx.split(date.strip()) produces year with 2 OR 4 digits

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

Sidebar

Related Questions

We have bunch of Domain Entities which should be rendered to an html format,
week DKK id=radio2 value=75 /> I have bunch of these div entry generated by
I have a bunch of console.log() calls in my JavaScript. Should I comment them
I have a bunch of these little bits of HTML code repeated over and
I have a bunch of Excel.Name objects under Workbook.Names collection which contain invalid references.
We have a excel file with a bunch of sheets. The first sheet is
I have bunch of web document and want to remove the html tags from
I am working on a PerformancePoint 2007 project. I have a bunch of Excel
Here's what I want to do... I have an excel with a bunch of
I have a big giant sql query where I select and format a bunch

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.