Good afternoon, i was wondering if anyone can assist me with this problem i am having. I am required to create a member function called void Date::Print() that prints 3 different formats of dates: 5/25/1995 , 05/25/1995, and May 25, 1995.
I need to set these as strings or any type that will allow me to store the data and use it for later because ill have to make a bool Date::SetFormat function as well to change the format. If there is anyone with any ideas, anything can help.
if this is for a homework exercise that requires you to create your own date class from scratch then you should follow kermort's advice of storing the date as 3 numbers to represent the year, month and day and create some parser for the format to be applied or alternatively have fixed format styles that can be applied without the need for parsing.
If this is a part required for a bigger project or work related then you can try the following by leveraging the functionality of an already existing date/time class that has a formatter:
1 2 3 4 5 6 7 8 9 10 11
class CMyDate : public KLib::KDate
KLib::KStr Format(constchar * szFmt) const;
staticconstchar * MonthNames;
A small point, make month = "ZERO" or something and do away with subtracting 1 to get the appropriate index, purely for convenience, clarity and simplicity.
Good point, and will also allow the exposing of the MonthNames array as public so as to be used from outside the class in a natural way without having to subtract 1 from the month first. Just ensure that month value passed as index to such an array falls within range ...