I am trying to use datetime objects, including datetime.month
, datetime.day
, and datetime.hour
.
The problem is that these objects (say datetime.month
) give values as 1, 2, 3, and so on to 12. Instead, I need these in the format 01,02,03 and so on to 12. There's a similar issue with days and months.
How can I switch to this format?
I realized this wasn't a very clear question:
I'm using string formatting to print values from a dictionary I have with timestamps.
So, the expression is roughly:
print "%s-%s-%s"%(date.year, date.month, date.day, etc., len(str) )
My values were originally in the correct "%Y-%m-%d
form (such as 2000-01-01). Using the above, I get 2000-1-1.
zfill is easier to remember:
In [19]: str(dt.month).zfill(2)
Out[19]: '07'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With