Wednesday, November 19, 2014

Python: Find out dates from Last Week.

Following code can be used to findout the Dates of Last week starting from monday
 import time  
 from datetime import date, timedelta  
 def foo(year, week):  
   d = date(year,1,1)  
   d = d - timedelta(d.weekday())  
   dlt = timedelta(days = (week)*7)  
   return d + dlt, d + dlt + timedelta(days=6)  
 todaysDate=time.localtime()  
 weekNumber=time.strftime("%W",todaysDate)  
 yearInTest=time.strftime("%Y",todaysDate)  
 print "Current Week Number is :::" + weekNumber  
 print "Current Year is :::" + yearInTest  
 lastWeeknumber=int(weekNumber)-1  
 print "Last Week Number is :::" + str(lastWeeknumber)  
 yearInTest=int(yearInTest)  
 dateInTest=foo(yearInTest,lastWeeknumber)  
 print str(dateInTest)  

More suggestion are always welcome. In case if you wish me to write about any specific scenario, please feel free to write to me or else you can Comment below.