Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datetime objects format

Tags:

python

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.

like image 927
ehertele Avatar asked Jul 24 '12 18:07

ehertele


1 Answers

zfill is easier to remember:

In [19]: str(dt.month).zfill(2)
Out[19]: '07'
like image 139
Daniil Mashkin Avatar answered Sep 29 '22 16:09

Daniil Mashkin