[Tutor] stripping 0's from 01's - 09's

Emile van Sebille emile at fenx.com
Thu Jun 4 21:39:16 CEST 2009


On 6/4/2009 12:04 PM Norman Khine said...
> Hello,
> Simple, I guess, but I am trying to do a formated date function, which
> I have working but for a small annoyance in that:
> 
>>>> from datetime import date
>>>> def format_date(day):
> ...     if day > 3 and day < 14:
> ...             return '%b %dth %Y'
> ...     indicator = day % 10
> ...     if indicator == 1:
> ...             return '%b %dst %Y'
> ...     elif indicator == 2:
> ...             return '%b %dnd %Y'
> ...     elif indicator == 3:
> ...             return '%b %drd %Y'
> ...     else:
> ...             return '%b %dth %Y'
> ...
>>>> today = '2009-06-04'
>>>> year, month, day = today.split('-')
>>>> date_object = date(int(year), int(month), int(day))
>>>> format = format_date(date_object.day)
>>>> formated_date = date_object.strftime(format)
>>>> formated_date
> 'Jun 04th 2009'
> 
> It will be much better to have:
> 
> 'Jun 4th 2009'
> 
> How would you go about in doing this?

Without reading the docs, you could just strip it out:

formated_date = date_object.strftime(format).replace(" 0"," ")

Emile



> 
> Thanks
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list