Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: give start and end of week data from a given date

Tags:

python

date

time

day = "13/Oct/2013" print("Parsing :",day) day, mon, yr= day.split("/") sday = yr+" "+day+" "+mon myday = time.strptime(sday, '%Y %d %b') Sstart = yr+" "+time.strftime("%U",myday )+" 0" Send = yr+" "+time.strftime("%U",myday )+" 6" startweek = time.strptime(Sstart, '%Y %U %w') endweek = time.strptime(Send, '%Y %U %w') print("Start of week:",time.strftime("%a, %d %b %Y",startweek)) print("End of week:",time.strftime("%a, %d %b %Y",endweek)) print("Data entered:",time.strftime("%a, %d %b %Y",myday))  out: Parsing : 13/Oct/2013 Start of week: Sun, 13 Oct 2013 End of week: Sat, 19 Oct 2013 Sun, 13 Oct 2013 

Learned python in the past 2 days and was wondering if there is a cleaner way to do this.This method works...it just looks ugly and It seems silly to have to create a new time variable for each date, and that there should be a way to offset the given date to the start and end of the week through a simple call but i have been unable to find anything on the internet or documentation that looks like it would work.

like image 816
shadowtdt09 Avatar asked Oct 07 '13 02:10

shadowtdt09


People also ask

How do I get the start date and end date in Python?

There are a few ways to do this, but I've gone with the following: last_date = datetime(year, month + 1, 1) + timedelta(days=-1) . This will calculate the first date of the following month, then subtract 1 day from it to get the last date of the current month.

How do I iterate through a date range in Python?

We can use the date_range() function method that is available in pandas. It is used to return a fixed frequency DatetimeIndex. We can iterate to get the date using date() function.

How do you get the day of the week from a date in Python?

isoweekday() to get a weekday of a given date in Python Use the isoweekday() method to get the day of the week as an integer, where Monday is 1 and Sunday is 7. i.e., To start from the weekday number from 1, we can use isoweekday() in place of weekday() . The output is 1, which is equivalent to Monday as Monday is 1.


2 Answers

Use the datetime module.

This will yield start and end of week (from Monday to Sunday):

from datetime import datetime, timedelta  day = '12/Oct/2013' dt = datetime.strptime(day, '%d/%b/%Y') start = dt - timedelta(days=dt.weekday()) end = start + timedelta(days=6) print(start) print(end) 

EDIT:

print(start.strftime('%d/%b/%Y')) print(end.strftime('%d/%b/%Y')) 
like image 153
Hyperboreus Avatar answered Sep 18 '22 14:09

Hyperboreus


Slight variation if you want to keep the standard time formatting and refer to the current day:

from datetime import date, timedelta  today = date.today() start = today - timedelta(days=today.weekday()) end = start + timedelta(days=6) print("Today: " + str(today)) print("Start: " + str(start)) print("End: " + str(end)) 
like image 40
palamunder Avatar answered Sep 19 '22 14:09

palamunder