How to convert int to Month and Year?

Exactly as the title states, how can I do this?
I have the conversion for the month down.
I cannot figure out how to find the day.
I got it working originally for January and February but March was screwing up.
So If I enter 33 I need to display February 2nd. Enter 363 will be December 29th, etc...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef INTTODAY_H
#define INTTODAY_H

class IntToDay
{
private:
	int Day;
public:
	IntToDay (int daynum) 
	{
		Day = daynum;
	};

	void print();


};


#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <iomanip>
#include "IntToDay.h"
using namespace std;

int main()
{
	int num;
	cout << "Please enter a day of the year: ";
	cin >> num;

	IntToDay convert(num);

	convert.print();




	return 0;
}


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <iomanip>
#include <string>
#include "IntToDay.h"
using namespace std;

void IntToDay::print()
{
	int x = 1;
	int Jan = 31;
	int Feb = 28 + Jan;
	int Mar = 31 + Feb;
	int Apr = 30 + Mar;
	int May = 31 + Apr;
	int Jun = 30 + May;
	int Jul = 31 + Jun;
	int Aug = 31 + Jul;
	int Sept = 30 + Aug;
	int Oct = 30 + Sept;
	int Nov = 30 + Oct;
	int Dec = 31 + Nov;
	
	int January[31], February[28], March[31], April[30], MayM[31], June[30], July[31];
	int August[31], September[30], October[30], November[30], December[31];

	for (int count = 0; count < 31; count++)
	{
		January[count] = x;
		March[count] = x;
		July[count] = x;
		MayM[count] = x;
		August[count] = x;
		December[count] = x;
		x++;
	}
	for (int count = 0; count < 30; count++)
	{
		April[count] = x;
		June[count] = x;
		September[count] = x;
		October[count] = x;
		November[count] = x;
		x++;
	}
	for (int count = 0; count < 28; count++)
	{
		February[count] = x;
		x++;
	}

	int MonthDays[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
	int indexOfMonth;
	if (Day >= 1 && Day <= Jan)
	{
		cout << "Month: January" << endl;
		int Temp = MonthDays[0] - Day + 1;
		int indexOfMonth = 31 - Temp;
		cout << "Day: " << January[indexOfMonth] << endl;
	}
	else if (Day > Jan && Day <= Feb) 
	{
		cout << "Month: February" << endl;
		int convert = (MonthDays[1] - Day);
		int Temp = (MonthDays[1] - convert);
		indexOfMonth = 28 - Temp;
		cout << "Day: " << February[indexOfMonth] << endl;
	}
	else if (Day > Feb && Day <= Mar)
	{
		cout << "Month: March" << endl;
		int Temp = (Mar - Day);
		indexOfMonth = Mar - Temp - 3;
		cout << "Day: " << March[indexOfMonth] << endl;
	}
	else if (Day > Mar && Day <= Apr)
	{
		cout << "Month: April" << endl;
		int Temp = (Apr - Day);
		indexOfMonth = Apr - Temp - 1;
		cout << "Day: " << April[indexOfMonth] << endl;
	}
	else if (Day > Apr && Day <= May)
	{
		cout << "Month: May" << endl;
		int Temp = (May - Day);
		indexOfMonth = May - Temp - 1;
		cout << "Day: " << MayM[indexOfMonth] << endl;
	}
	else if (Day > May && Day <= Jun)
	{
		cout << "Month: June" << endl;
		int Temp = (Jun - Day);
		indexOfMonth = Jun - Temp - 1;
		cout << "Day: " << June[indexOfMonth] << endl;
	}
	else if (Day > Jun && Day <= Jul)
	{
		cout << "Month: July" << endl;
		int Temp = (Jul - Day);
		indexOfMonth = Jul - Temp - 1;
		cout << "Day: " << July[indexOfMonth] << endl;
	}
	else if (Day > Jul && Day <= Aug)
	{
		cout << "Month: Aug" << endl;
		int Temp = (Aug - Day);
		indexOfMonth = Aug - Temp - 1;
		cout << "Day: " << August[indexOfMonth] << endl;
	}
	else if (Day > Aug && Day <= Sept)
	{
		cout << "Month: September" << endl;
		int Temp = (Sept - Day);
		indexOfMonth = Sept - Temp - 1;
		cout << "Day: " << September[indexOfMonth] << endl;
	}
	else if (Day > Sept && Day <= Oct)
	{
		cout << "Month: October" << endl;
		int Temp = (Oct - Day);
		indexOfMonth = Oct - Temp - 1;
		cout << "Day: " << October[indexOfMonth] << endl;
	}
	else if (Day > Oct && Day <= Nov)
	{
		cout << "Month: November" << endl;
		int Temp = (Nov - Day);
		indexOfMonth = Nov - Temp - 1;
		cout << "Day: " << November[indexOfMonth] << endl;
	}
	else if (Day > Nov && Day <= Dec)
	{
		cout << "Month: December" << endl;
		int Temp = (Dec - Day);
		indexOfMonth = Dec - Temp - 1;
		cout << "Day: " << December[indexOfMonth] << endl;
	}
}
All months, except feb, have fixed number of days – so we use 3 vectors – one for jan, one for marTodec and one for feb depending on whether or not its a leap year.
Given leap year or not the appropriate vectors are combined (using move instead of copy) to make a year vector and then the day and month are read off using std::find and std::distance – read up these functions and shout if anything's unclear:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
    std::vector<std::string> jan(31, "Jan");
    //for (const auto & elem : jan)std::cout << elem << " ";std::cout << "\n";
    std::cout << "jan size: " << jan.size() << "\n";//sanity checks
    std::vector<std::string> marTodec{306};
    auto itr = marTodec.begin();
    std::fill(marTodec.begin(), itr + 31, "Mar");
    itr += 31;
    std::fill(itr, itr + 30, "Apr");
    itr += 30;
    std::fill(itr, itr+31, "May");
    itr += 31;
    std::fill(itr, itr+30, "Jun");
    itr += 30;
    std::fill(itr, itr+31, "Jul");
    itr += 31;
    std::fill(itr, itr+31, "Aug");
    itr += 31;
    std::fill(itr, itr+30, "Sep");
    itr += 30;
    std::fill(itr, itr + 31, "Oct");
    itr += 31;
    std::fill(itr, itr+30, "Nov");
    itr+= 30;
    std::fill(itr, itr+31, "Dec");
   // for (const auto& elem : marTodec) std::cout << elem << " ";std::cout << "\n";
    std::cout << "marTodec size: " << marTodec.size() << "\n";
    bool leap = true;
    if(leap)
    {
        std::vector<std::string> feb(29, "feb");
       // std::cout << "feb size: " << feb.size() << "\n";
        std::vector<std::string> year {};
        year.reserve(jan.size() + feb.size() + marTodec.size());
        year.insert(year.end(), std::make_move_iterator(jan.begin()), std::make_move_iterator(jan.end()));
        year.insert(year.end(), std::make_move_iterator(feb.begin()), std::make_move_iterator(feb.end()));
        year.insert(year.end(), std::make_move_iterator(marTodec.begin()), std::make_move_iterator(marTodec.end()));
        std::cout << "year size: " << year.size() << "\n";
        //for (const auto& elem : year)std::cout << elem << " "; std::cout << "\n";

        std::cout << "Enter day of the year \n";
        size_t day{};
        std::cin >> day;
        auto itrMonth = std::find(year.begin(), year.end(), year[day-1]);
        auto itrDay = year.begin() + day;

        std::cout << "The day is: " << year[day - 1] << " " << std::distance(itrMonth, itrDay);
    }
    else
    {
        std::vector<std::string> feb(28, "feb");
       // std::cout << "feb size: " << feb.size() << "\n";
        std::vector<std::string> year {};
        year.reserve(jan.size() + feb.size() + marTodec.size());
        year.insert(year.end(), std::make_move_iterator(jan.begin()), std::make_move_iterator(jan.end()));
        year.insert(year.end(), std::make_move_iterator(feb.begin()), std::make_move_iterator(feb.end()));
        year.insert(year.end(), std::make_move_iterator(marTodec.begin()), std::make_move_iterator(marTodec.end()));
        std::cout << "year size: " << year.size() << "\n";
        //for (const auto& elem : year)std::cout << elem << " "; std::cout << "\n";

        std::cout << "Enter day of the year \n";
        size_t day{};
        std::cin >> day;
        auto itrMonth = std::find(year.begin(), year.end(), year[day-1]);
        auto itrDay = year.begin() + day;

        std::cout << "The day is: " << year[day - 1] << " " << std::distance(itrMonth, itrDay);
     }
}
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
36
37
38
#include <iostream>
#include <string>

int main()
{
    enum { JAN = 1, FEB = 2, DEC = 12 };

    // cumulative_days[0] == 0 is a place holder; does not correspond to an actual month
    int cumulative_days[DEC+1] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } ;
    // add up the days to make it cumulative
    for( int i = JAN ; i <= DEC ; ++i ) cumulative_days[i] += cumulative_days[i-1] ;

    // JAN == 1, DEC == 12, month_name[0] is unused
    const std::string month_name[DEC+1] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } ;
    int nday ;
    std::cout << "day of year? " ;
    std::cin >> nday ;

    if( nday > cumulative_days[FEB] ) // if it is later than 28 FEB
    {
        char leap ; // we need to know if it is a leap year
        std::cout << "is it a leap year (y/n)? " ;
        std::cin >> leap ;

        if( leap == 'y' || leap == 'Y' ) // add one to FEB onwards for leap year
            for( int i = FEB ; i <= DEC ; ++i ) ++cumulative_days[i] ;
    }

    if( nday > 0 && nday <= cumulative_days[DEC] ) // if valid
    {
        int month = JAN ;
        while( nday > cumulative_days[month] ) ++month ; // get to the right month

        // print out the name of the month and the day in the month
        std::cout << month_name[month] << ' ' << nday - cumulative_days[month-1] << '\n' ;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <ctime>
#include <iomanip>

int main()
{
    const auto now = std::time(nullptr) ;
    std::tm tm = *std::localtime( std::addressof(now) ) ;

    int nday ;
    std::cout << "days since january 01, " << 1900+tm.tm_year << ": " ;
    std::cin >> nday ;

    tm.tm_mon = 0 ; // january
    tm.tm_mday = nday ;
    tm.tm_hour = 12 ; // noon

    const auto t_nday = std::mktime( std::addressof(tm) ) ;
    std::cout << std::put_time( std::localtime( std::addressof(t_nday) ), "%d %B, %Y\n" ) ;
}
Topic archived. No new replies allowed.