Returning Date As String ?
Roy Smith
roy at panix.com
Sat May 14 07:36:07 EDT 2005
Peter Moscatt <pgmoscatt at optushome.com.au> wrote:
> I am wanting to return the system date using the following:
>
> date.today()
>
> How would I then convert this to a string ?
>
> Pete
datetime.data objects have a __str__() method. To convert one to a string,
you just have to cause its __str__() to be invoked. The most common ways
of doing that are str(), %s, or print:
>>> import datetime
>>> d = datetime.date.today()
>>> d
datetime.date(2005, 5, 14) # this is the repr() of the object
>>> print d
2005-05-14
>>> str(d)
'2005-05-14'
>>> '%s' % d
'2005-05-14'
More information about the Python-list
mailing list