import datetime as dtfrom dateutil.relativedelta import relativedelta#get first and last day of this and last month)today = dt.date.today()first_day_this_month = dt.date(day=1, month=today.month, year=today.year)last_day_last_month = first_day_this_month - relativedelta(days=1)print (first_day_this_month, last_day_last_month)
>2015-03-01 2015-02-28
class myDate:
def __init__(self):self.day = 0self.month = 0self.year = 0## for checking valid days month and yearwhile (True):d = int(input("Enter The day :- "))if (d > 31):print("Plz 1 To 30 value Enter ........")else:self.day = dbreak
while (True):m = int(input("Enter The Month :- "))if (m > 13):print("Plz 1 To 12 value Enter ........")else:self.month = mbreak
while (True):y = int(input("Enter The Year :- "))if (y > 9999 and y < 0000):print("Plz 0000 To 9999 value Enter ........")else:self.year = ybreak## method for aday ands cnttract daysdef adayDays(self, n):## aday days to date daynd = self.day + nprint(nd)## check days subtract from dateif nd == 0: ## check if days are 7 subtracted from 7 then,........if(self.year % 4 == 0):if(self.month == 3):self.day = 29self.month -= 1self.year = self. yearelse:if(self.month == 3):self.day = 28self.month -= 1self.year = self. yearif (self.month == 5) or (self.month == 7) or (self.month == 8) or (self.month == 10) or (self.month == 12):self.day = 30self.month -= 1self.year = self. year
elif (self.month == 2) or (self.month == 4) or (self.month == 6) or (self.month == 9) or (self.month == 11):self.day = 31self.month -= 1self.year = self. year
elif(self.month == 1):self.month = 12self.year -= 1## nd == 0 if condition over## after subtract days to day io goes into negative thenelif nd < 0 :n = abs(n)## return positive if no is negativefor i in range (n,0,-1): ##
if self.day == 0:
if self.month == 1:self.day = 30
self.month = 12self.year -= 1else:self.month -= 1if(self.month == 1) or (self.month == 3)or (self.month == 5) or (self.month == 7) or (self.month == 8) or (self.month == 10) or (self.month ==12):self.day = 30elif(self.month == 4)or (self.month == 6) or (self.month == 9) or (self.month == 11):self.day = 29elif(self.month == 2):if(self.year % 4 == 0):self.day == 28else:self.day == 27else:self.day -= 1
## enf of elif negative days## adaying days to DATEelse:cnt = 0while (True):
if self.month == 2: # check leap year
if(self.year % 4 == 0):if(nd > 29):cnt = nd - 29nd = cntself.month += 1else:self.day = ndbreak## if not leap year thenelse:
if(nd > 28):cnt = nd - 28nd = cntself.month += 1else:self.day = ndbreak## checking month other than february monthelif(self.month == 1) or (self.month == 3) or (self.month == 5) or (self.month == 7) or (self.month == 8) or (self.month == 10) or (self.month == 12):if(nd > 31):cnt = nd - 31nd = cnt
if(self.month == 12):self.month = 1self.year += 1else:self.month += 1else:self.day = ndbreak
elif(self.month == 4) or (self.month == 6) or (self.month == 9) or (self.month == 11):if(nd > 30):cnt = nd - 30nd = cntself.month += 1
else:self.day = ndbreak## end of month condition## end of while loop## end of else condition for adaying daysdef formatDate(self,frmt):
if(frmt == 1):ff=str(self.day)+"-"+str(self.month)+"-"+str(self.year)elif(frmt == 2):ff=str(self.month)+"-"+str(self.day)+"-"+str(self.year)elif(frmt == 3):ff =str(self.year),"-",str(self.month),"-",str(self.day)elif(frmt == 0):print("Thanky You.....................")
else:print("Enter Correct Choice.......")print(ff)
dt = myDate()nday = int(input("Enter No. For Aday or SUBTRACT Days :: "))dt.adayDays(nday)print("1 : day-month-year")print("2 : month-day-year")print("3 : year-month-day")print("0 : EXIT")frmt = int (input("Enter Your Choice :: "))dt.formatDate(frmt)
enter code here