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
Author
calender ライブラリのmonthrangeがつかえそう。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
次月、前月を返す関数をつくりたい。timedeltaとreplace組み合わせればできそう。