none
Help about code to sum Y/M/D RRS feed

  • Frage

  • Hello, friends.

    I have a form like this:

    This is a form to calculate work experience in 30-days base.

    Example ( Y/M/D ):

    3/8/24 + 5/3/19 + 2/9/8 = 11/9/21

    Please help me with code to calculate.

    Regards

    Mittwoch, 11. Dezember 2019 08:54

Antworten

  • Hi veso dimov,
    As Viorel_ said, you can convert all the values to days.
    Here is a code example and you can refer to it.

    private void button1_Click(object sender, EventArgs e)
    {
        int d1 = Convert.ToInt32(textBox_d1.Text);
        int d2 = Convert.ToInt32(textBox_d2.Text);
        int d3 = Convert.ToInt32(textBox_d3.Text);
        int dsum = d1 + d2 + d3;
    
        int m1 = Convert.ToInt32(textBox_m1.Text);
        int m2 = Convert.ToInt32(textBox_m2.Text);
        int m3 = Convert.ToInt32(textBox_m3.Text);
        int msum = m1 + m2 + m3;
    
        int y1 = Convert.ToInt32(textBox_y1.Text);
        int y2 = Convert.ToInt32(textBox_y2.Text);
        int y3 = Convert.ToInt32(textBox_y3.Text);
        int ysum = y1 + y2 + y3;
    
        int DaySum = ysum * 12 * 30 + msum * 30 + dsum;
        int Y = DaySum / (12 * 30);
        int M = (DaySum - (Y * 12 * 30)) / 30;
        int D = DaySum - (Y * 12 * 30 + M * 30);
    
        textBox_Y.Text = Y.ToString();
        textBox_M.Text = M.ToString();
        textBox_D.Text = D.ToString();
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Donnerstag, 12. Dezember 2019 06:49

Alle Antworten

  • Hello,

    You need to make an effort first, once done present your code and indicate where you are having issues/


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Mittwoch, 11. Dezember 2019 11:25
    Moderator
  • Hello.

    Of course, you're right. If it comes to ordinary actions, I do it, but here it is complicated because of the transfer of days to months and months to years / 35 days need to stay 1 month and 5 days, 14 monts and 39 days need to be 1 year, 3 monts and 9 days ... /. And I really have no idea where to start. Thanks for understanding.

    Mittwoch, 11. Dezember 2019 14:11
  • Maybe start by converting all the values to days. For example, “3/8/24” becomes 3 * 12 * 30 + 8 * 30 + 24 days.

    Then sum these days.

    Then convert the sum to “Y/M/D” using, for example, the next expressions (in VB):

       Y = Sum \ (12 * 30)

       M = (Sum Mod (12 * 30)) \ 30

       D = Sum Mod 30

    Or these:

       Y = Sum \ (12 * 30)

       M = (Sum - (Y * 12 * 30)) \ 30

       D = Sum - (Y * 12 * 30 + M * 30)

    Mittwoch, 11. Dezember 2019 19:10
  • Hi veso dimov,
    As Viorel_ said, you can convert all the values to days.
    Here is a code example and you can refer to it.

    private void button1_Click(object sender, EventArgs e)
    {
        int d1 = Convert.ToInt32(textBox_d1.Text);
        int d2 = Convert.ToInt32(textBox_d2.Text);
        int d3 = Convert.ToInt32(textBox_d3.Text);
        int dsum = d1 + d2 + d3;
    
        int m1 = Convert.ToInt32(textBox_m1.Text);
        int m2 = Convert.ToInt32(textBox_m2.Text);
        int m3 = Convert.ToInt32(textBox_m3.Text);
        int msum = m1 + m2 + m3;
    
        int y1 = Convert.ToInt32(textBox_y1.Text);
        int y2 = Convert.ToInt32(textBox_y2.Text);
        int y3 = Convert.ToInt32(textBox_y3.Text);
        int ysum = y1 + y2 + y3;
    
        int DaySum = ysum * 12 * 30 + msum * 30 + dsum;
        int Y = DaySum / (12 * 30);
        int M = (DaySum - (Y * 12 * 30)) / 30;
        int D = DaySum - (Y * 12 * 30 + M * 30);
    
        textBox_Y.Text = Y.ToString();
        textBox_M.Text = M.ToString();
        textBox_D.Text = D.ToString();
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Donnerstag, 12. Dezember 2019 06:49
  • Thank you, friends, for help.

    Maybe I should explain further - An employee has internships in several companies and the task is to collect the total internship. For example: in first company have a 3y/8m/17d, in second - 4y/7m/14d, in 3-th 1y/9m/22d. All internship need to be 10y/1m/23d, because 30 days equals 1 month and 12 months - 1 year.

    I think somethink like this /for a start/:

    NumericUpDown17.Value = (NumericUpDown2.Value + NumericUpDown5.Value + NumericUpDown8.Value + NumericUpDown11.Value + NumericUpDown14.Value)
            NumericUpDown18.Value = NumericUpDown3.Value + NumericUpDown6.Value + NumericUpDown9.Value + NumericUpDown12.Value + NumericUpDown15.Value
    
            If NumericUpDown18.Value > 30 Then NumericUpDown17.Value = (NumericUpDown2.Value + NumericUpDown5.Value + NumericUpDown8.Value + NumericUpDown11.Value + NumericUpDown14.Value) + 1
            If NumericUpDown18.Value > 30 Then NumericUpDown18.Value = (NumericUpDown3.Value + NumericUpDown6.Value + NumericUpDown9.Value + NumericUpDown12.Value + NumericUpDown15.Value) - 30
    Or am I wrong?

    Donnerstag, 12. Dezember 2019 07:16
  • Thank you, Daniel_Zhang. This is on Visual Basic ?

    I receive a error BC30800. I try to declare d1 as String, but no result.

    Maybe because i work with NumericUpDown ... ?

    Donnerstag, 12. Dezember 2019 10:08
  • Hi veso dimov,
    This is coded on visual studio using c#.
    I used the NumericUpDown control to test and it is succesful.

    int d1 = Convert.ToInt32(numericUpDown1.Text);

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Freitag, 13. Dezember 2019 06:25
  • Thank you, Daniel, but this project is on Visual Studio using Visual Basic.

    I edit your code and have this:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim d1 = Convert.ToInt32(TextBox3.Text)
            Dim d2 = Convert.ToInt32(TextBox6.Text)
            Dim d3 = Convert.ToInt32(TextBox9.Text)
            Dim dsum = d1 + d2 + d3
    
            Dim m1 = Convert.ToInt32(TextBox2.Text)
            Dim m2 = Convert.ToInt32(TextBox5.Text)
            Dim m3 = Convert.ToInt32(TextBox8.Text)
            Dim msum = m1 + m2 + m3
    
            Dim y1 = Convert.ToInt32(TextBox1.Text)
            Dim y2 = Convert.ToInt32(TextBox4.Text)
            Dim y3 = Convert.ToInt32(TextBox7.Text)
            Dim ysum = y1 + y2 + y3
    
            Dim DaySum = ysum * 12 * 30 + msum * 30 + dsum
    
            Dim Y = DaySum / (12 * 30)
            Dim M = (DaySum - (Y * 12 * 30)) / 30
            Dim D = DaySum - (Y * 12 * 30 + M * 30)
    
            TextBox10.Text = Y.ToString()
            TextBox11.Text = M.ToString()
            TextBox12.Text = D.ToString()

    But result with your data is not real:

    • Bearbeitet veso dimov Freitag, 13. Dezember 2019 08:04 Edit text
    Freitag, 13. Dezember 2019 07:33
  • Hi veso dimov,
    In Visual Basic, you should code like this.

    Dim Y = DaySum \(12 * 30)
    Dim M = (DaySum - (Y * 12 * 30)) \ 30

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Freitag, 13. Dezember 2019 08:42
  • Thank you, friends, for your support.

    Now all work correct. Code is:

    Dim d1 = Convert.ToInt32(TextBox3.Text)
            Dim d2 = Convert.ToInt32(TextBox6.Text)
            Dim d3 = Convert.ToInt32(TextBox9.Text)
            Dim dsum = d1 + d2 + d3
    
            Dim m1 = Convert.ToInt32(TextBox2.Text)
            Dim m2 = Convert.ToInt32(TextBox5.Text)
            Dim m3 = Convert.ToInt32(TextBox8.Text)
            Dim msum = m1 + m2 + m3
    
            Dim y1 = Convert.ToInt32(TextBox1.Text)
            Dim y2 = Convert.ToInt32(TextBox4.Text)
            Dim y3 = Convert.ToInt32(TextBox7.Text)
            Dim ysum = y1 + y2 + y3
    
            Dim DaySum = ysum * 12 * 30 + msum * 30 + dsum
    
            Dim Y = DaySum \ (12 * 30)
            Dim M = (DaySum - (Y * 12 * 30)) \ 30
            Dim D = DaySum - (Y * 12 * 30 + M * 30)
    
            TextBox10.Text = Y.ToString()
            TextBox11.Text = M.ToString()
            TextBox12.Text = D.ToString()

    Best regards !

    Veso Dimov


    • Bearbeitet veso dimov Freitag, 13. Dezember 2019 09:20
    Freitag, 13. Dezember 2019 09:19