Changing The format of a date

I need help with changing the format of a date like this (1/1/2008) to Jan 1, 2008 and 01/01/2008.

In my code I have a two functions for the class called date, one is SetFormat() and the other is Show().

SetFormat is supposed to take in 1 of three chars 'D' for default, 'T' for two digit, and 'L' for long.
Default is (m/d/y) in integer form, Two digit is (mm/dd/y) in integer form, and
Long is (month name day, year)

The SetFormat is supposed to affect the Show() function by changing its format.
Show is automatically set to default whether or not the SetFormat is called or not.

Here is my code:
void Date::Show()
{

cout << month << "/" << day << "/" << year << endl;

}

bool Date::SetFormat(char f)
{
if('L')
{
return true;
}
else if ('D')
{
return true;
}
else if ('T')
{
return true;
}
else
return false;
}

There are a couple of things wrong with your SetFormat() function.
1) You pass in f, but you don't do anything with it. You probably want to store it in a member variable.
2) Your if statements aren't checking f. if ('L') is always going to return true. You probably meant:
if (f == 'L')

Your Show() function only outputs a single format. You want to check for the saved format and output each format accordingly.

BTW, it always a good idea to post the full declaration of your class so we can see what member variables and functions you have.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Let std::strftime do the heavy lifting. http://en.cppreference.com/w/cpp/chrono/c/strftime

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <ctime>
#include <string>

std::string format( int year, int month, int day, char flag = 'D' )
{
    std::tm tm {} ;
    tm.tm_year = year - 1900 ;
    tm.tm_mon = month - 1 ;
    tm.tm_mday = day ;

    std::string fmt ;

    if( flag == 'T' || flag == 't' ) fmt = "%m/%d/%y" ; // month/day/year
    else if( flag == 'L' || flag == 'l' ) fmt = "%A %B %d, %Y" ; // long

    if( !fmt.empty() )
    {
        char buffer[128] ;
        std::strftime( buffer, sizeof(buffer), fmt.c_str(), std::addressof(tm) ) ;
        return buffer ;
    }
    else return std::to_string(month) + '/' + std::to_string(day) + '/' + std::to_string(year) ;
}

int main()
{
    const int year = 2017 ;
    const int month = 9 ;
    const int day = 30 ;

    std::cout << "default: " << format( year, month, day ) << '\n'         // default: 9/30/2017
              << "  short: " << format( year, month, day, 'T' ) << '\n'    //   short: 09/30/17
              << "   long: " << format( year, month, day, 'L' ) << '\n' ;  //    long: Sunday September 30, 2017
}

http://coliru.stacked-crooked.com/a/9768510c6d17599f
Topic archived. No new replies allowed.