469,622 Members | 2,114 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,622 developers. It's quick & easy.

Returning Date As String ?

I am wanting to return the system date using the following:

date.today()

How would I then convert this to a string ?

Pete

Jul 19 '05 #1
3 29239
if you're using date.today():
x=date.today()

Jul 19 '05 #2
Peter Moscatt <pg*******@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'
Jul 19 '05 #3
Peter Moscatt wrote:
I am wanting to return the system date using the following:

date.today()

How would I then convert this to a string ?


Use the strftime method. The formatting directives are documented here:
http://docs.python.org/lib/module-time.html

Example: date.today().strftime("%B %d, %Y")

--
Brian Beck
Adventurer of the First Order
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by praba kar | last post: by
reply views Thread by Fritz | last post: by
9 posts views Thread by fpvt2 | last post: by
3 posts views Thread by Jef Driesen | last post: by
2 posts views Thread by GaryDean | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.