Remove Leading 0's For Str(date)
If I have a datetime object, how would I get the date as a string in the following format: 1/27/1982 # it cannot be 01/27/1982 as there can't be leading 0's The current way I'm d
Solution 1:
You could format it yourself instead of using strftime
:
'{0}/{1}/{2}'.format(d.month, d.day, d.year) // Python 2.6+'%d/%d/%d' % (d.month, d.day, d.year)
Solution 2:
The datetime object has a method strftime(). This would give you more flexibility to use the in-built format strings.
http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior.
I have used lstrip('0')
to remove the leading zero.
>>> d = datetime.datetime(1982, 1, 27)
>>> d.strftime("%m/%d/%y")
'01/27/82'
>>> d.strftime("%m/%d/%Y")
'01/27/1982'
>>> d.strftime("%m/%d/%Y").lstrip('0')
'1/27/1982'
Post a Comment for "Remove Leading 0's For Str(date)"