Using Python, get the day of the current month as an integer

Question:

I need to write a method that returns 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

Asked By: ben55

||

Answers:

>>> import datetime
>>> datetime.datetime.today().day
Answered By: programmersbook
from datetime import datetime

today = datetime.now()
today.day # this is a integer
Answered By: diegueus9

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.

Answered By: Tim

This is similar to the answer by programmersbook. It uses datetime.date rather than datetime.datetime.

>>> import datetime
>>> datetime.date.today().day
8
Answered By: Asclepius
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.