Archive
Posts Tagged ‘today’
date today
October 17, 2010
Leave a comment
Let’s see how to get today’s date in the format yyyymmdd, i.e. {year}{month}{day}. This format has an advantage. If you have several dates like this and you sort them lexicographically, then you get them in chronological order. I often use this format when creating subdirectories in the file system. Most file managers sort them automatically, so I can see them in order.
#!/usr/bin/env python
import datetime
def date_to_str(d):
return ''.join(str(i) for i in d)
today = datetime.date.today().timetuple()[:3]
print today # (2010, 10, 17)
print date_to_str(today) # 20101017
Here today is a tuple with three elements. The function date_to_str() joins the elements and returns a string. If you use the separator ‘-‘, i.e. '-'.join(...), then you get the following output: 2010-10-17.
Good to know
The form year before month before day is standard in Asian countries, Hungary, Sweden and the US armed forces.
