Format Setting for Dates

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.
what do you have so far ?
closed account (48T7M4Gy)
A format arrangement operates on the basic data (properties).

So what this means is you first have to decide what constitutes a date. A date can be a set of 3 numbers, a day number, a month number and a year number.

Also, once you know the month number you can convert the number into a month name eg month[5] = "May".

Put all that together and you can produce the output you require.

PS why does your method return a bool?

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:

MyDate.h
1
2
3
4
5
6
7
8
9
10
11
#include "KDate.h"


class CMyDate : public KLib::KDate
{
public:
   KLib::KStr Format(const char * szFmt) const;

private:
   static const char * MonthNames[12];
};


MyDate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "MyDate.h"


using namespace KLib;


const char * CMyDate::MonthNames[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};


KStr CMyDate::Format(const char * szFmt) const
{
   KStr strDate = KDate::ToString(szFmt);
   strDate.Replace(KStr::Str("%donth", GetMonth()).sz(), MonthNames[GetMonth()-1]);
   return strDate;
}


Tst_MyDate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include "KLibIncUse.h"
#include "MyDate.h"


int main()
{
   int nYear = 1995;
   int nMonth = 5;
   int nDay = 25;
   
   CMyDate dt;
   dt.SetDate(nYear, nMonth, nDay, 0, 0, 0);
   printf("dt = [%s]\n", dt.ToString().sz());
   
   printf("dt = [%s]\n", dt.Format("%M/%D/%Y").sz());          //to output -- 5/25/1995
   printf("dt = [%s]\n", dt.Format("%02M/%02D/%Y").sz());      //to output -- 05/25/1995
   printf("dt = [%s]\n", dt.Format("%Month %02D, %Y").sz());   //to output -- May 25, 1995
}
closed account (48T7M4Gy)
A small point, make month[0] = "ZERO" or something and do away with subtracting 1 to get the appropriate index, purely for convenience, clarity and simplicity.
A small point, make month[0] = "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 ...
Topic archived. No new replies allowed.