2

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.

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
Liza
  • 123
  • 1
  • 10
  • [datetime.strptime()](http://docs.python.org/library/datetime.html#strftime-strptime-behavior) is all you need. – Jacob Jul 26 '11 at 11:03
  • Are you really dealing with dates in the 19th century? That's going to create trouble when you want to reformat the date back to readable date using strftime(). – Susam Pal Jul 26 '11 at 11:10
  • @ Susam Pal: yes, some are before 1900 & some are after 1900 – Liza Jul 26 '11 at 11:12

4 Answers4

2

Nor time.strftime nor datetime.datetime.strftime don't accept year values less then 1900. So it looks like there is job for re (although it is a bad style to use it for such tasks):

>>> import re
>>> s = 'February 27, 1820'
>>> month, day, year = re.findall('(\w+) (\d+), (\d+)', s)[0]  
>>> '%s %s %s' % (day, month, year)
'27 February 1820'
Liza
  • 123
  • 1
  • 10
Roman Bodnarchuk
  • 29,461
  • 12
  • 59
  • 75
  • How to get `27 February 1820` by your solution? – Liza Jul 26 '11 at 11:46
  • @Liza Simply writing ``'%s %s %s' % (day, month, year)`` – eyquem Jul 26 '11 at 11:54
  • probabaly, `'%s %s %s' % (month, year, day)`. Anyway, Thanks a lot! – Liza Jul 26 '11 at 12:05
  • 1
    @Liza Warning: test the Roman's code on the following string ``'On February 27, 1820 there were 10, 1000, 100000, maybe more shooting stars in the 98, 456347XBC, Hi-Pot galaxy that were seen by 20, 2000 or 200000 persons'`` and you'll see the problem – eyquem Jul 26 '11 at 12:57
1

Use datetime.datetime.strptime, or parsedatetime, or dateutil to parse your string to date.

Then use datetime.datetime.strftime to format your datetime object to string using needed format.

warvariuc
  • 57,116
  • 41
  • 173
  • 227
1

You can use strptime / strftime:

from datetime import datetime

date = datetime.strptime('February 27, 1820', '%B %d, %Y')
new_date = date.strftime('%d %b %Y')

However, there's one gotcha. strftime seems to have problems with years < 1900 on some platforms. There's a workaround for it too.

Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
1

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.

Community
  • 1
  • 1
eyquem
  • 26,771
  • 7
  • 38
  • 46