Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Datetime - Get first and last day of the current week #2384

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
295 changes: 295 additions & 0 deletions Datetime/Datetime_Get_first_and_last_day_of_the_current_week.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73f2337a-d83b-4fc0-ad5e-25e6afa0a9d3",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"cell_type": "markdown",
"id": "d21f3261-0ede-4a5c-b50b-eb566a042090",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# Datetime - Get first and last day of the current week"
]
},
{
"cell_type": "markdown",
"id": "bfed5b78-8d35-460e-a6d0-9115cd52762c",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #datetime #python #week #current #first #last"
]
},
{
"cell_type": "markdown",
"id": "091e87f5-0fc6-4f64-a5e7-82cc040e9dcb",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/)"
]
},
{
"cell_type": "markdown",
"id": "244012bd-24b4-4b92-9ebe-06c3b85faf85",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-11-16 (Created: 2023-11-16)"
]
},
{
"cell_type": "markdown",
"id": "cff29e68-62c5-4a3b-af31-fa00579962b3",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook shows how to get the first and last day of the current week using Python."
]
},
{
"cell_type": "markdown",
"id": "ce3aede6-73cb-45a1-bf97-205a30137421",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n",
"- [Python datetime](https://docs.python.org/3/library/datetime.html)\n",
"- [Python timedelta](https://docs.python.org/3/library/datetime.html#timedelta-objects)"
]
},
{
"cell_type": "markdown",
"id": "73332542-a39b-4ff6-b287-b2728ed48fd0",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "aab415f0-7975-4c24-b356-57adf8c02b2b",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cc0cb25c-fb3e-41b0-954b-9489595f8824",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-16T08:30:01.209606Z",
"iopub.status.busy": "2023-11-16T08:30:01.209158Z",
"iopub.status.idle": "2023-11-16T08:30:01.219278Z",
"shell.execute_reply": "2023-11-16T08:30:01.218709Z",
"shell.execute_reply.started": "2023-11-16T08:30:01.209532Z"
},
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"import datetime"
]
},
{
"cell_type": "markdown",
"id": "e401b9b9-7fde-4a35-8148-84f5e9cc57f3",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n",
"- `today`: today's date"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ee9859fc-e30d-4ed5-a080-cb767ef7f0e0",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-16T08:30:01.220769Z",
"iopub.status.busy": "2023-11-16T08:30:01.220545Z",
"iopub.status.idle": "2023-11-16T08:30:01.411130Z",
"shell.execute_reply": "2023-11-16T08:30:01.410530Z",
"shell.execute_reply.started": "2023-11-16T08:30:01.220739Z"
},
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"today = datetime.date.today()"
]
},
{
"cell_type": "markdown",
"id": "7ee18647-21fa-483c-9df3-9205ed865cb0",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "407f8d0f-013d-403e-bdf1-1a3ce07cba24",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Get first and last day of the current week"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cac70dd2-7c55-43eb-8d37-706aea280190",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-16T08:30:01.412274Z",
"iopub.status.busy": "2023-11-16T08:30:01.412096Z",
"iopub.status.idle": "2023-11-16T08:30:01.532052Z",
"shell.execute_reply": "2023-11-16T08:30:01.531396Z",
"shell.execute_reply.started": "2023-11-16T08:30:01.412254Z"
},
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"# Get the day of the week as an integer, where Monday is 0 and Sunday is 6\n",
"day_of_week = today.weekday()\n",
"# Subtract day_of_week from today to get the first day of the current week\n",
"first_day_of_week = today - datetime.timedelta(days=day_of_week)\n",
"# Add 6 to get the last day of the current week\n",
"last_day_of_week = first_day_of_week + datetime.timedelta(days=6)"
]
},
{
"cell_type": "markdown",
"id": "bd03ed57-8bd2-4d00-89f3-c414c6bae648",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "88375c34-dfee-4736-a79a-1b3fcb9a0da9",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Display result"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "94f5f1a5-26c2-4db8-930d-d606d42b24f5",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-16T08:30:01.533126Z",
"iopub.status.busy": "2023-11-16T08:30:01.532868Z",
"iopub.status.idle": "2023-11-16T08:30:01.819949Z",
"shell.execute_reply": "2023-11-16T08:30:01.819245Z",
"shell.execute_reply.started": "2023-11-16T08:30:01.533099Z"
},
"papermill": {},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First day of the current week: 2023-11-13\n",
"Last day of the current week: 2023-11-19\n"
]
}
],
"source": [
"print(f\"First day of the current week: {first_day_of_week}\")\n",
"print(f\"Last day of the current week: {last_day_of_week}\")"
]
},
{
"cell_type": "markdown",
"id": "3c8c9b5a-2210-4833-b066-f83de19dbb0a",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
" "
]
}
],
"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.9.6"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading