Date time in python

python has a very strong date time library,which one can handle different pattern of date time string.

I was working in a project user will submit the date time in unusual format and I have to convert it in a pattern

like:”yy-month-day”.

I just went through pyhton doc,unfortunately missed its power.And started to write custom function for doing the task.

My function was like below:

</pre>
def date_convert(raw_format):
>>> def date_convert(raw_format):
    date_list = raw_format.split()
    temp_m = date_list[0].strip(",")
    spacer = "-"
    month_list = ["January","February","March","April", "May", "June","July","August","Septempber","October","November","December"]
    raw_month_number = str(month_list.index(temp_m)+1)
    date_fix = (date_list[2],raw_month_number,date_list[1].strip(","))
    final_date = (spacer).join(date_fix)
    return final_date
>>> print date_convert("March 12, 2010")
2010-3-12

After coding this method,I asked one of my seniors about any other date time formatting are available or not in this issues in terms of performance,he gave me
two links :
1.http://seehuhn.de/pages/pdate
2.http://docs.python.org/dev/library/datetime.html
Using above two links easy and efficient function can be written.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s