date reformatting

B

Bell, Kevin

Anyone aware of existing code to turn a date string "8-15-05" into the
number 20050815?

The dateutil module has a parse method that looks perfect, I downloaded
and unzipped it, but could figure out how to install it. I using
windows XP and py2.4.

Any ideas?


Kev
 
L

Larry Bates

There's more than one way, but this one will work for
dates prior to 2000.

import time
datestring='8-15-05'
d=time.strptime(datestring,'%m-%d-%y')
number=10000*d[0]+100*d[1]+d[2]
print number

-Larry Bates
 
L

Larry Bates

There's more than one way, but this one will work for
dates prior to 2000.

import time
datestring='8-15-05'
d=time.strptime(datestring,'%m-%d-%y')
number=10000*d[0]+100*d[1]+d[2]
print number

-Larry Bates
 
M

Magnus Lycka

Anyone aware of existing code to turn a date string "8-15-05" into the
number 20050815?
'20050815'

Of course, if you really want the *number* 20050815 you'd
have to do
Using a datetime.date object means that you have good
support for a lot of arithmetic on and formatting of
dates without writing a lot of new code.

If you really mean that you want the number 20050815, I
assume this is because some legacy system beyond you control
need to have dates in that format. It's not a particularly
good format for dates. If you just want a numeric storage of
dates, I'd suggest using datetime.date.toordinal/fromordinal.
Those numbers aren't as easy to decipher manually, but at
least they work right if you subtract dates or add or subtract
days from a date.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,342
Members
48,018
Latest member
DelilahDen

Latest Threads

Top