goldb.org home

AS OF MAY 2008, THIS BLOG IS NO LONGER BEING UPDATED.
Visit the new blog at: http://coreygoldberg.blogspot.com



 Monday, March 03, 2008

Python - Padding Single Digits In Dates

Here is how to zero-pad single digit days or months in a date string:


date = '3/2/2008'
padded_date = time.strftime('%m/%d/%Y', time.strptime(date,'%m/%d/%Y'))
print padded_date
>> 03/02/2008
#    Comments [2] |
Monday, March 03, 2008 4:14:29 PM (Eastern Standard Time, UTC-05:00)
Better yet, use ISO 8601 format: %Y-%m-%d (e.g. 2008-03-01). Completely unambiguous: big to small. Sortable. Comprehensible by those outside of the USA (is your example February 3 or March 2? it's ambiguous).

http://en.wikipedia.org/wiki/ISO_8601
Monday, March 03, 2008 4:20:27 PM (Eastern Standard Time, UTC-05:00)
@David:
good point .. thanks :)

> is your example February 3 or March 2? it's ambiguous)
.. and you thought us Americans even know other date formats exist :)
Comments are closed.