[Tutor] simplifying with string-formatting operator

paul brian paul1brian at gmail.com
Fri Sep 23 17:41:04 CEST 2005


I would suggest you use the built in datetime modules or egenix mx.DateTime.

for example

>>> import datetime
>>> year = "05"; month="09";day="23"
>>> dateobj = datetime.date(int(year)+2000, int(month), int(day))
>>> dateobj.strftime("%A %B %y")
'Friday September 05'
>>>

obviously you need to look at how the incoming 2 digit strings are
checked and so on.

By the way the strftime("%A %B %y") tells python to print out the date
as %A which is locale weekday etc etc. with the %A or %B being
replaced in a similar way to %s in normal string formatting.

strftime is well documented in the time module docs.

cheers


On 9/23/05, Goofball223 at wmconnect.com <Goofball223 at wmconnect.com> wrote:
> Hello
>
> Does anyone have any idea on how i could simplify the following program by
> using strings?
>
> # dateconvert2.py
> #    Converts day month and year numbers into two date formats
>
> import string
>
> def main():
>    # get the day month and year
>    day, month, year = input("Please enter the day, month and year numbers:
> ")
>
>    date1 = str(month)+"/"+str(day)+"/"+str(year)
>
>    months = ["January", "February", "March", "April",
>              "May", "June", "July", "August",
>              "September", "October", "November", "December"]
>    monthStr = months[month-1]
>    date2 = monthStr+" " + str(day) + ", " + str(year)
>
>    print "The date is", date1, "or", date2
>
> main()
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


--
--------------------------
Paul Brian
m. 07875 074 534
t. 0208 352 1741


More information about the Tutor mailing list