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


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:

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")

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 :
Using above two links easy and efficient function can be written.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.