Skip to content

Instantly share code, notes, and snippets.

@yuu-ito
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save yuu-ito/4ea826208ab6f381fb37 to your computer and use it in GitHub Desktop.

Select an option

Save yuu-ito/4ea826208ab6f381fb37 to your computer and use it in GitHub Desktop.
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
@yuu-ito
Copy link
Copy Markdown
Author

yuu-ito commented Sep 9, 2014

次月、前月を返す関数をつくりたい。timedeltaとreplace組み合わせればできそう。

@yuu-ito
Copy link
Copy Markdown
Author

yuu-ito commented Sep 9, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment