Change Date

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#pragma once
#include<iostream>
#include<cmath>
#include <ctime>

namespace CppCLRWinformsProjekt {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

struct Date
{
   int d, m, y;
};

const int monthDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

/// <summary>
/// Zusammenfassung für Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Konstruktorcode hier hinzufügen.
//
}

protected:
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::TextBox^ textBox2;

private:
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
void InitializeComponent(void)
{
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(112, 65);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(177, 23);
this->textBox1->TabIndex = 0;
//
// button1
//
this->button1->Location = System::Drawing::Point(31, 65);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::Button1_Click);
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(112, 108);
this->textBox2->Multiline = true;
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(177, 23);
this->textBox2->TabIndex = 2;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(360, 328);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion

#pragma region Adding Date

bool isLeap(int y)
{
if (y % 100 != 0 && y % 4 == 0 || y % 400 == 0)
return true;

return false;
}

int offsetDays(int d, int m, int y)
{
int offset = d;

switch (m - 1)
{
case 11:
offset += 30;
case 10:
offset += 31;
case 9:
offset += 30;
case 8:
offset += 31;
case 7:
offset += 31;
case 6:
offset += 30;
case 5:
offset += 31;
case 4:
offset += 30;
case 3:
offset += 31;
case 2:
offset += 28;
case 1:
offset += 31;
}

if (isLeap(y) && m > 2)
offset += 1;

return offset;
}

void revoffsetDays(int offset, int y, int* d, int* m)
{
int month[13] = { 0, 31, 28, 31, 30, 31, 30,
 31, 31, 30, 31, 30, 31 };

if (isLeap(y))
month[2] = 29;

int i;
for (i = 1; i <= 12; i++)
{
if (offset <= month[i])
break;
offset = offset - month[i];
}

*d = offset;
*m = i;
}

void addDays(int d1, int m1, int y1, int x)
{
int offset1 = offsetDays(d1, m1, y1);
int remDays = isLeap(y1) ? (366 - offset1) : (365 - offset1);
int y2, offset2;
if (x <= remDays)
{
y2 = y1;
offset2 = offset1 + x;
}

else
{
x -= remDays;
y2 = y1 + 1;
int y2days = isLeap(y2) ? 366 : 365;
while (x >= y2days)
{
x -= y2days;
y2++;
y2days = isLeap(y2) ? 366 : 365;
}
offset2 = x;
}

int m2, d2;
revoffsetDays(offset2, y2, &d2, &m2);
textBox1->Text += d2 + " - " + m2 + " - " + y2;
}
#pragma endregion

#pragma region Subtract Date
int countLeapYears(Date d)
{
int years = d.y;
if (d.m <= 2)
years--;
return years / 4 - years / 100 + years / 400;
}

int getDifference(Date dt1, Date dt2)
{
long int n1 = dt1.y * 365 + dt1.d;
for (int i = 0; i < dt1.m - 1; i++)
n1 += monthDays[i];
n1 += countLeapYears(dt1);
long int n2 = dt2.y * 365 + dt2.d;
for (int i = 0; i < dt2.m - 1; i++)
n2 += monthDays[i];
n2 += countLeapYears(dt2);
return (n2 - n1);
}
#pragma endregion

private: System::Void Button1_Click(System::Object^ sender, System::EventArgs^ e)
{
int d = 1, m = 1, y = 1970;
int x = 18257;

addDays(d, m, y, x);

int d2, m2, y2;
time_t t = time(NULL);
tm* tPtr = localtime(&t);

Date dt1 = { 27, 12, 2019 }; // old
Date dt2 = { tPtr->tm_mday, (tPtr->tm_mon) + 1, (tPtr->tm_year) + 1900 }; // current

textBox2->Text += "Total difference : " + getDifference(dt1, dt2);
}
};
}
What is your question?

Looking at your code, I suggest a couple of utility functions:
1
2
// Return the number of days elapsed from some fixed date to the "date" parameter.
int toDays(Date &date);

In other words, this contains the code in getDifference() to compute n1 and n2.

The second function does the reverse:
1
2
// Return the Date corresponding to the number of days elapsed from our fixed base date 
Date fromDays(int day);


Once you have these functions, date manipulations are easy. Another advantage is that you can test the functions very thoroughly. Just run through every day for the past few hundred years converting day->date->day. if the resulting day is different from the starting day then you have a bug.

Many of the Form1() methods should actually be methods of your Date structure.

I need to change the output from textBox1:

textBox1->Text += d2 + " - " + m2 + " - " + y2;

to be int so i can write it to:

Date dt1 = { 27, 12, 2019 };

Because at there i type it manually.

I need to make Date dt1 = { d2, m2, y2}; but always get error
Last edited on
I found the answer. I just to erase initialitation of d2,m2,y2 from void to on the top program, at line 21 maybe :) And it works
Topic archived. No new replies allowed.