lisa lisa - 4 months ago 11
Python Question

Adding month, day, year to a date

I need to write a class syntax, to be able to add days, months.. to a date.
currently I only have this:

class Date:
def __init__(self, day=1, month=1, year=2015):
self.day = day
self.mon = month
self.year = year
def printUS(self):
print self.mon , "/" , self.day , "/" , self.year
def printUK(self):
print self.day , "." , self.mon , "." , str(self.year)[2:]
def AddDay(self,n=1):


I am confused how to write the last function in a way to add days for each month correctly not to exceed the days of month or year.
I rather not use any other modules, since I have not learned them at all.
I don't have more than a month basic programming experience,and never had previous experience before.

Answer

Try this.


class Date(object):
    def __init__(self, day=1, month=1, year=2015):
        self.day = day
        self.mon = month
        self.year = year
    def __str__(self):
        return "Date is: %s %s %s"%(self.day,self.mon,self.year)
    def IsLeapYear(self):
        """
        function returns 1 if self.year is leap,0 if it's not
        """
        if(not (self.year%400) or (not(self.year%4) and self.year%100)):
            return 1
        else:
            return 0
    #values by each month whether it's leap year or not
    monthlength = (
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    )

        #the function you probably were looking for
    def AddDay(self, n):
        days=n
        leap_year=self.IsLeapYear()
        while(self.day+days>=Date.monthlength[leap_year][self.mon-1]):
            days=days-(Date.monthlength[leap_year][self.mon-1]-self.day)
            self.day=0
            self.mon+=1
            if (self.mon>12):
                self.mon=1
                self.year+=1
            leap_year=self.IsLeapYear()
        else:
            self.day+=days
Comments