brand new here!
This is my first time working with Python classes and I am a bit confused. I wanted to try and store dates with three attributes - year, month, and day (as integers).
What I want to do is:
for an ____init____() function to take a year, month and a date (with defaults 1900, 1 and 1) and return it with the month written out:
>>date_one = Date( 1973, 5, 2)
May 2, 1973
>>date_two = Date( 1984)
January 1, 1984
for a ____str____() function to format the data as a string with year, month, and day:
>>date_one = Date( 1973, 5, 2)
>>string = str(date_one)
>>string
'1973-05-02'
>>date_two = Date( 1984)
>>print date_two
1984-01-01
for same_date_in_year() to determine whether two dates fall on the same date, even if they aren't in the same year
>>date_one = Date( 1972, 3, 27)
>>date_two = Date( 1998, 4, 17)
>>date_three = Date(1957, 4, 17)
>>date_one.same_date_in_year(date_two)
False
>>date_two.same_date_in_year(date_three)
True
What I have so far:
days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month_names = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
class Date(object):
year = 1900
month = 1
day = 1
def __init__(self, year=1900, month=1, day=1):
self.year, self.month, self.day = year, month, day
print month_names[month], str(day)+', '+str(year)
def __str__(self, year=1900, month=1, day=1):
self.year, self.month, self.day = year, month, day
print str(year)+'-'+str(month).rjust(2,'0')+'-'+str(day).rjust(2,'0')
def same_date_in_year(year, month, day):
pass
if __name__ == "__main__":
date_one = Date( 1972, 3, 27 )
date_two = Date( 1998, 4, 13 )
date_three = Date( 1996, 4, 13 )
print "date_one is " + str(date_one)
print "date_two is " + str(date_two)
print "date_three is " + str(date_three)
print "date_one.same_day_in_year(date_two)", date_one.same_day_in_year(date_two)
print "date_two.same_day_in_year(date_three)", date_two.same_day_in_year(date_three)
print
I have no idea how the Class would work and the functions inside it, before the pass. If someone could help me, it would be much appreciated!