Date Abstract Data Type
Date Abstract Data Type
A Date Abstract Data Type (ADT) is a conceptual model that defines the
properties and operations for managing dates, abstracting away the
implementation details. The Date ADT encapsulates the idea of a calendar date,
typically comprising year, month, and day components. It provides a set of
operations that can be performed on these date values.
Code:
Basic Code:
# various formats
print("\n")
format1 = current.strftime("%m/%d/%y")
format2 = current.strftime("%b-%d-%Y")
format3 = current.strftime("%d/%m/%Y")
Current Day is : 23
Current Month is : 3
format1 = 03/23/21
format2 = Mar-23-2021
format3 = 23/03/2021
Next Code
class DateADT:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
def __repr__(self):
return
f"{self.year:04d}-{self.month:02d}-{self.day:02d}"
def is_leap_year(self):
if (self.year % 4 == 0 and self.year % 100 != 0) or
(self.year % 400 == 0):
return True
return False
self.day += days
while self.day > days_in_month[self.month - 1]:
self.day -= days_in_month[self.month - 1]
self.month += 1
if self.month > 12:
self.month = 1
self.year += 1
# Example usage:
date = DateADT(2024, 6, 29)
print(date)
date.add_days(5)
print(date)
print(date.is_leap_year())
O/P
Output: 2024-06-29
Output: 2024-07-04
Output: True