Skip to content

Instantly share code, notes, and snippets.

@shotahorii
Created April 27, 2019 13:31
Show Gist options
  • Save shotahorii/0324263ee6ab616a2e5746ab704fee56 to your computer and use it in GitHub Desktop.
Save shotahorii/0324263ee6ab616a2e5746ab704fee56 to your computer and use it in GitHub Desktop.
GetDate
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import datetime"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def get_date(day_of_week,week_lag):\n",
" # day_of_week: the day of the week as an integer, where Monday is 0 and Sunday is 6.\n",
" # week_lag: number of weeks to get lagged\n",
" now = datetime.datetime.now()\n",
" return (now.date() \n",
" - datetime.timedelta(days=now.weekday())\n",
" +datetime.timedelta(days=day_of_week,weeks=-week_lag)\n",
" ).strftime('%Y-%m-%d')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'2019-04-19'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Last Friday = Friday(4) of last week(1)\n",
"get_date(4,1)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment