简体   繁体   中英

how to get number of days in month based on date string input

I am trying to automate a code. I would like to have the code pull data starting at the beginning of the month to end of the previous day. Currently I am using the following command to get the enddate:

dateEnd = pd.to_datetime('today')

How do tell the code, based on what today is to go back to the beginning of the month? AND, how do I tell the code if its the first of the month to return the previous months data?

For a bonus, once I have the start and end date, how do return find the number of days in the month? I have tried this command, but it does not want to work on a single date.

startTime_date.dt.daysinmonth

This will give you the wanted dates:

import datetime

end = datetime.date.today() - datetime.timedelta(1)
start = end.replace(day=1)
daysInMonth = (datetime.date(start.year, start.month + 1, 1) - datetime.timedelta(1)).day

start
#2018-10-01

end
#2018-10-09

daysInMonth
#31

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