lisa
lisa

Reputation: 13

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.

Upvotes: 1

Views: 1069

Answers (5)

Andy Case
Andy Case

Reputation: 76

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

Upvotes: 0

Rolf Lussi
Rolf Lussi

Reputation: 615

to keep it very simple you could make an array with the length of each month.

monthlength = [31, 28, 31, 30, 31, ...]

And then check which month it is in your add day function. so in the end looks like this:

monthlength = [31, 28, 31, 30, 31, ...]

def AddDay(self, n=1):
    if self.day + n > self.monthlength[self.month]:
         self.day = self.day + n - self.monthlength[self.month]
         self.month = self.month + 1
         if self.month == 13:
              self.month = 1
              self.year = self.year + 1
              if self.year%4 == 0:
                  self.monthlength[1] = 29
              else:
                  self.monthlength[1] = 28

    else:
         self.day = self.day + n

Upvotes: 0

lisa
lisa

Reputation: 13

class Date:
    def __init__(self, day, month, year=2015):
        self.day = day
        self.month = month
        self.year = year
        self.monthlength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    def printUS(self):
        print self.month , "/" , self.day , "/" , self.year
    def printUK(self):
        print self.day , "." , self.month , "." , str(self.year)[2:]
    def AddDay(self,n=1):
        if (self.day) + n > (self.monthlength[self.month]):
                self.day = self.day + n - self.monthlength[self.month]
            else:
                self.day = self.day + n
import sys
mydate=Date(1,11,2015)
mydate.AddDay(31)
mydate.printUS()  

Upvotes: 0

motoku
motoku

Reputation: 1581

You are off to a good start. I recommend looking into the modulo operator. You can use it to increment months after calculating the number of days in a given month after reading this question. If you have any questions, feel free to comment them.

Upvotes: 1

Darren Ringer
Darren Ringer

Reputation: 278

For that you could create a function that determines the number of days in a given month; i.e. num_days(month, year) -> int numOfDays

Then you could simply change the method to your Date class, add_days(self, n=1), to check the value of num_days(self.mon, self.year) and then if self.day + n is greater than that value, increment self.mon and subtract the value from n, using the difference as the new value for self.day.

The real trick is creating the num_days function, because in leap years February will have a different number of days. I recommend looking up the full technical definition of a leap year because it's slightly more complicated than just checking if it's divisible by 4.

Upvotes: 0

Related Questions