MatLab can take a date and move it to the end of the month, quarter, etc. using the dateshift(...)
function .
Is there an equivalent function in Python?
I'm not sure if it counts as the same, but the datetime
module can make times available as a timetuple
, with separate components for year, month, day etc. This is easy to manipulate directly to advance to the next month, etc.
If you don't mind using an extension, check out dateutil
. The list of features starts with:
- Computing of relative deltas (next month, next year, next monday, last week of month, etc);
The documentation for dateutil.relativedelta
shows how to advance to various points.
I think calendar.monthrange
should do it for you, eg:
>>> import datetime, calendar
>>> year = int(input("Enter year: "))
Enter year: 2012
>>> month = int(input("Enter month: "))
Enter month: 3
>>> endOfMonthDate = datetime.date(year, month, calendar.monthrange(year, month)[1])
>>> endOfMonthDate
datetime.date(2012, 3, 31)
>>>
You might find other helpful functions here: https://docs.python.org/2/library/calendar.html and here: https://docs.python.org/2/library/datetime.html
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.