Last active
August 29, 2015 14:06
-
-
Save yuu-ito/4ea826208ab6f381fb37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| import calendar | |
| def lastday_of_month(date): | |
| y = date.year | |
| m = date.month | |
| last_d = calendar.monthrange(y, m)[1] | |
| return date.replace(day=last_d) | |
| def next_month(date): | |
| delta = datetime.timedelta(days=1) | |
| return lastday_of_month(date) + delta | |
| def prev_month(date): | |
| delta = datetime.timedelta(days=1) | |
| first_day_of_month = date.replace(day=1) | |
| prev_last_month = first_day_of_month - delta | |
| return prev_last_month.replace(day=1) | |
| # import datetime as dt | |
| # today = dt.date.today() | |
| # print "Today: " + today.strftime('%Y-%m-%d') | |
| # one_day = dt.timedelta(days=1) | |
| # print today+one_day | |
| # print today-one_day | |
| # print today-one_day*31 #.replace(days=1) | |
| # pre_month = today-one_day*31 #.replace(days=1) | |
| # pre_month = pre_month.replace(day=1) | |
| # print pre_month |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://success.tracpath.com/blog/2009/07/09/python%E3%81%A7%E3%81%AE%E6%97%A5%E4%BB%98%E9%96%A2%E9%80%A3%E5%87%A6%E7%90%86/
calender ライブラリのmonthrangeがつかえそう。