Python: give start and end of week data from a given date -
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 past 2 days , wondering if there cleaner way this.this method works...it looks ugly , seems silly have create new time variable each date, , there should way offset given date start , end of week through simple call have been unable find on internet or documentation looks work.
use datetime
module.
this yield start , end of week (from monday 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'))
Comments
Post a Comment