简体   繁体   中英

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

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

>>> 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM