Skip to content

Instantly share code, notes, and snippets.

@includeamin
Created March 19, 2019 21:16
Show Gist options
  • Save includeamin/ef8c11df7015f78ecd103f6b62e0807a to your computer and use it in GitHub Desktop.
Save includeamin/ef8c11df7015f78ecd103f6b62e0807a to your computer and use it in GitHub Desktop.
python datetime some types
days = ['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
today_date = datetime.datetime.today()
last30date = today_date - datetime.timedelta(days=30)
todayname = today_date.strftime("%A")
startweek = ""
endweek = ""
if todayname == days[0]:
startweek = today_date
else:
index = days.index(todayname)
startweek = today_date - datetime.timedelta(days=index)
endweek = today_date + datetime.timedelta(days=(len(days)-index))
startOflastweek = startweek - datetime.timedelta(days=6)
endOflastweek = startweek - datetime.timedelta(days=1)
startOfthisMonth = datetime.datetime(today_date.year, today_date.month, 1)
startOfLastMonth = startOfthisMonth - datetime.timedelta(days=30)
endOfLastMonth = startOfLastMonth + datetime.timedelta(days=29)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment