Convert Date to Short formate

A

aqmaiya

Hello,
there is string value 'Dec 06, 2000' I want to convert that string
date to SHORT FORMAT like '2000-12-06-. Please help me how do I do
that? I'm new in Jython.
Thanks,
aqmaiya
 
F

Fredrik Lundh

aqmaiya said:
there is string value 'Dec 06, 2000' I want to convert that string
date to SHORT FORMAT like '2000-12-06-. Please help me how do I do
that?

you mean ISO format, right?

the easiest way is probably to use the time.strptime module to parse the
original string into a time tuple, and then use time.strftime to format
the tuple as an ISO date:
'2000-12-06'

also see:

http://docs.python.org/lib/module-time.html

</F>
 
J

John Machin

Hello,
there is string value 'Dec 06, 2000' I want to convert that string
date to SHORT FORMAT like '2000-12-06-. Please help me how do I do
that? I'm new in Jython.
Thanks,
aqmaiya

Two ways (at least):

(1) check out the strptime and strftime (p == parse, f == format)
functions in the time module

In general, the datetime module is much to be preferred for working with
dates and times -- unless of course you need to mimic functions from the
C time.h library -- however the datetime module won't be getting a
strptime until version 2.5, which is still in alpha test.

(2) As you are new to Python, you might like to try your skills with
basic parts from the Python toolkit, like dictionaries and slicing, and
write a function yourself, specialised to that particular format. Here
are some hints:

# example Dec 06, 2000
# ruler 0123456789012

mpart = data[0:3]
# similarly: dpart, ypart

month_dict = {'jan': 1, ....., 'dec': 12}
month_num = month_dict[mpart.lower()] # will work with Dec, dec, DEC

result = '%s-%02d-%s' % (ypart, month_num, dpart)

How much validation you do is up to you :)

HTH,
John
 

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
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top