Using Python, get the day of the current month as an integer
I need to write a method that retur开发者_JAVA技巧ns the day of the month as an integer. For example, if it's Feb 8th 2011, I want to have method like this:
>>> day = get_day_of_month()
where day would be given the integer value 8
>>> import datetime
>>> datetime.datetime.today().day
from datetime import datetime
today = datetime.now()
today.day # this is a integer
Or the "old school" (lower overhead, if it matters) method...
import time
time.localtime(time.time())[2]
time.localtime() returns a tuple containing all of the elements of a timestamp.
This is similar to the answer by programmersbook. It uses datetime.date
rather than datetime.datetime
.
>>> import datetime
>>> datetime.date.today().day
8
精彩评论