WinForm Calculate all gridview to date











up vote
0
down vote

favorite












I want calculate date values like the image.



enter image description here



And there is any simple way to add footer in MetroGridView that calculate like label value?

I'm currently using label to show total values.

Any suggests or ideas?

-Thanks



Here is the code:



private void btnCalc_Click(object sender, EventArgs e)
{
// check start date
if (dtStart.Value > dtEnd.Value)
{
MetroMessageBox.Show(this, "Start Date wrong!", "Error");
return;
}

DateTime Now = dtEnd.Value;
int Years = new DateTime(Now.Subtract(dtStart.Value).Ticks).Year - 1;
DateTime PastYearDate = dtStart.Value.AddYears(Years);
int Months = 0;

for (int i = 1; i <= 12; i++)
{
if (PastYearDate.AddMonths(i) == Now)
{
Months = i;
break;
}
else if (PastYearDate.AddMonths(i) >= Now)
{
Months = i - 1;
break;
}
}
int Days = Now.Subtract(PastYearDate.AddMonths(Months)).Days;

int index = metroGrid1.Rows.Add();
metroGrid1.Rows[index].Cells[0].Value = Years;
metroGrid1.Rows[index].Cells[1].Value = Months;
metroGrid1.Rows[index].Cells[2].Value = Days;

for (int i = 0; i < metroGrid1.Rows.Count; ++i)
{
Years += Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value);
Months += Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value);
Days += Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value);

// stuck here ...

lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";
}
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    I want calculate date values like the image.



    enter image description here



    And there is any simple way to add footer in MetroGridView that calculate like label value?

    I'm currently using label to show total values.

    Any suggests or ideas?

    -Thanks



    Here is the code:



    private void btnCalc_Click(object sender, EventArgs e)
    {
    // check start date
    if (dtStart.Value > dtEnd.Value)
    {
    MetroMessageBox.Show(this, "Start Date wrong!", "Error");
    return;
    }

    DateTime Now = dtEnd.Value;
    int Years = new DateTime(Now.Subtract(dtStart.Value).Ticks).Year - 1;
    DateTime PastYearDate = dtStart.Value.AddYears(Years);
    int Months = 0;

    for (int i = 1; i <= 12; i++)
    {
    if (PastYearDate.AddMonths(i) == Now)
    {
    Months = i;
    break;
    }
    else if (PastYearDate.AddMonths(i) >= Now)
    {
    Months = i - 1;
    break;
    }
    }
    int Days = Now.Subtract(PastYearDate.AddMonths(Months)).Days;

    int index = metroGrid1.Rows.Add();
    metroGrid1.Rows[index].Cells[0].Value = Years;
    metroGrid1.Rows[index].Cells[1].Value = Months;
    metroGrid1.Rows[index].Cells[2].Value = Days;

    for (int i = 0; i < metroGrid1.Rows.Count; ++i)
    {
    Years += Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value);
    Months += Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value);
    Days += Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value);

    // stuck here ...

    lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";
    }
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want calculate date values like the image.



      enter image description here



      And there is any simple way to add footer in MetroGridView that calculate like label value?

      I'm currently using label to show total values.

      Any suggests or ideas?

      -Thanks



      Here is the code:



      private void btnCalc_Click(object sender, EventArgs e)
      {
      // check start date
      if (dtStart.Value > dtEnd.Value)
      {
      MetroMessageBox.Show(this, "Start Date wrong!", "Error");
      return;
      }

      DateTime Now = dtEnd.Value;
      int Years = new DateTime(Now.Subtract(dtStart.Value).Ticks).Year - 1;
      DateTime PastYearDate = dtStart.Value.AddYears(Years);
      int Months = 0;

      for (int i = 1; i <= 12; i++)
      {
      if (PastYearDate.AddMonths(i) == Now)
      {
      Months = i;
      break;
      }
      else if (PastYearDate.AddMonths(i) >= Now)
      {
      Months = i - 1;
      break;
      }
      }
      int Days = Now.Subtract(PastYearDate.AddMonths(Months)).Days;

      int index = metroGrid1.Rows.Add();
      metroGrid1.Rows[index].Cells[0].Value = Years;
      metroGrid1.Rows[index].Cells[1].Value = Months;
      metroGrid1.Rows[index].Cells[2].Value = Days;

      for (int i = 0; i < metroGrid1.Rows.Count; ++i)
      {
      Years += Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value);
      Months += Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value);
      Days += Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value);

      // stuck here ...

      lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";
      }
      }









      share|improve this question















      I want calculate date values like the image.



      enter image description here



      And there is any simple way to add footer in MetroGridView that calculate like label value?

      I'm currently using label to show total values.

      Any suggests or ideas?

      -Thanks



      Here is the code:



      private void btnCalc_Click(object sender, EventArgs e)
      {
      // check start date
      if (dtStart.Value > dtEnd.Value)
      {
      MetroMessageBox.Show(this, "Start Date wrong!", "Error");
      return;
      }

      DateTime Now = dtEnd.Value;
      int Years = new DateTime(Now.Subtract(dtStart.Value).Ticks).Year - 1;
      DateTime PastYearDate = dtStart.Value.AddYears(Years);
      int Months = 0;

      for (int i = 1; i <= 12; i++)
      {
      if (PastYearDate.AddMonths(i) == Now)
      {
      Months = i;
      break;
      }
      else if (PastYearDate.AddMonths(i) >= Now)
      {
      Months = i - 1;
      break;
      }
      }
      int Days = Now.Subtract(PastYearDate.AddMonths(Months)).Days;

      int index = metroGrid1.Rows.Add();
      metroGrid1.Rows[index].Cells[0].Value = Years;
      metroGrid1.Rows[index].Cells[1].Value = Months;
      metroGrid1.Rows[index].Cells[2].Value = Days;

      for (int i = 0; i < metroGrid1.Rows.Count; ++i)
      {
      Years += Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value);
      Months += Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value);
      Days += Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value);

      // stuck here ...

      lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";
      }
      }






      c# winforms gridview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago

























      asked 2 days ago









      Tamiraa Aquarius

      123




      123
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          It seems to me that you should put this statement :



          lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";



          outside for loop.



          And in for loop, you use three Int: "Years", "Months", "Days", to represent the accumulation of the time span, but actually they are Int, not Timespan, so after the loop, it could be like: 5 years 20 months 78 days, I assume that is not the kind of result you want.



          Why don't you use TimeSpan type to replace those three Int ?



          Edit:
          You could use the code below:



          // never mind the date of s1 (1990,1,1),it's not important, just as a start point for the Timespan ts1 to do accumulate;
          DateTime s1 = new DateTime(1990, 1, 1);
          TimeSpan ts1 = new TimeSpan();


          for (int i = 0; i < metroGrid1.Rows.Count; ++i)
          {
          ts1 += s1.AddDays(Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value)).AddMonths(Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value)).AddYears(Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value)) - s1;
          }


          int daysResult;
          int monthsResult;
          int yearsResult;


          // get the total number of days, then /365 to get the year count;
          yearsResult = ts1.Days /365;

          // get the month count by /30 from the remainder of /365;
          monthsResult = (ts1.Days % 365) / 30;

          // get the day count from the remainder of /30;
          daysResult = (ts1.Days % 365) % 30;

          lbltotal.Text = "Total: " + yearsResult.ToString() + " years, " + monthsResult.ToString() + " months and " + daysResult.ToString() + " days";





          share|improve this answer























          • Thanks for updating your answer. It works but it calculates bit different. When I enter start date: 2017.05.01, end date: 2018.10.31 it would be: 1 year, 5 months and 30 days but in label: 1 year 6 months and 3 days. I will try resolve this problem using your code. Thanks for your hard work :)
            – Tamiraa Aquarius
            2 days ago


















          up vote
          0
          down vote













          I have tested Kuo-hsuan Hsu solution and it returns:
          Total: 19 years, 8 months and 3 days



          I tried a little bit similar (see below) and get:
          Total: 19 years, 7 months and 26 days
          Windows calculator tells me: 19 Years, 7 Month, 3 weeks, 5 days what is the same as above. But anyway, over a long period my code is not exact and Kuo-hsuan Hsu code isen't it as well.



                 // taken values from grid
          int years = 18 ;
          int months = 18 ;
          int days = 57 ;
          DateTime today = DateTime.Today; // 2018-11-17
          DateTime futuredate = today.AddYears(years);
          futuredate = futuredate.AddMonths(months);
          futuredate = futuredate.AddDays(days); // 2038-07-13

          TimeSpan tsp = futuredate - today;
          years = tsp.Days / 365;
          futuredate = futuredate.AddYears(-years);
          tsp = futuredate - today;
          months = tsp.Days / 30;
          futuredate = futuredate.AddMonths(-months);
          tsp = futuredate - today;
          days = tsp.Days;
          string res1 = "Total: " + years.ToString() + " years, " + months.ToString() + " months and " + days.ToString() + " days";


          I am not happy with the code above.
          I am thinking to run in an loop from start date to end date and calculate the values.
          That would enable me even to count only working days - maybe having a list of public holidays for the next 100 years.






          share|improve this answer























          • Thanks for reply. This one is calculate for past not future :)
            – Tamiraa Aquarius
            2 days ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350250%2fwinform-calculate-all-gridview-to-date%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          It seems to me that you should put this statement :



          lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";



          outside for loop.



          And in for loop, you use three Int: "Years", "Months", "Days", to represent the accumulation of the time span, but actually they are Int, not Timespan, so after the loop, it could be like: 5 years 20 months 78 days, I assume that is not the kind of result you want.



          Why don't you use TimeSpan type to replace those three Int ?



          Edit:
          You could use the code below:



          // never mind the date of s1 (1990,1,1),it's not important, just as a start point for the Timespan ts1 to do accumulate;
          DateTime s1 = new DateTime(1990, 1, 1);
          TimeSpan ts1 = new TimeSpan();


          for (int i = 0; i < metroGrid1.Rows.Count; ++i)
          {
          ts1 += s1.AddDays(Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value)).AddMonths(Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value)).AddYears(Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value)) - s1;
          }


          int daysResult;
          int monthsResult;
          int yearsResult;


          // get the total number of days, then /365 to get the year count;
          yearsResult = ts1.Days /365;

          // get the month count by /30 from the remainder of /365;
          monthsResult = (ts1.Days % 365) / 30;

          // get the day count from the remainder of /30;
          daysResult = (ts1.Days % 365) % 30;

          lbltotal.Text = "Total: " + yearsResult.ToString() + " years, " + monthsResult.ToString() + " months and " + daysResult.ToString() + " days";





          share|improve this answer























          • Thanks for updating your answer. It works but it calculates bit different. When I enter start date: 2017.05.01, end date: 2018.10.31 it would be: 1 year, 5 months and 30 days but in label: 1 year 6 months and 3 days. I will try resolve this problem using your code. Thanks for your hard work :)
            – Tamiraa Aquarius
            2 days ago















          up vote
          1
          down vote



          accepted










          It seems to me that you should put this statement :



          lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";



          outside for loop.



          And in for loop, you use three Int: "Years", "Months", "Days", to represent the accumulation of the time span, but actually they are Int, not Timespan, so after the loop, it could be like: 5 years 20 months 78 days, I assume that is not the kind of result you want.



          Why don't you use TimeSpan type to replace those three Int ?



          Edit:
          You could use the code below:



          // never mind the date of s1 (1990,1,1),it's not important, just as a start point for the Timespan ts1 to do accumulate;
          DateTime s1 = new DateTime(1990, 1, 1);
          TimeSpan ts1 = new TimeSpan();


          for (int i = 0; i < metroGrid1.Rows.Count; ++i)
          {
          ts1 += s1.AddDays(Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value)).AddMonths(Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value)).AddYears(Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value)) - s1;
          }


          int daysResult;
          int monthsResult;
          int yearsResult;


          // get the total number of days, then /365 to get the year count;
          yearsResult = ts1.Days /365;

          // get the month count by /30 from the remainder of /365;
          monthsResult = (ts1.Days % 365) / 30;

          // get the day count from the remainder of /30;
          daysResult = (ts1.Days % 365) % 30;

          lbltotal.Text = "Total: " + yearsResult.ToString() + " years, " + monthsResult.ToString() + " months and " + daysResult.ToString() + " days";





          share|improve this answer























          • Thanks for updating your answer. It works but it calculates bit different. When I enter start date: 2017.05.01, end date: 2018.10.31 it would be: 1 year, 5 months and 30 days but in label: 1 year 6 months and 3 days. I will try resolve this problem using your code. Thanks for your hard work :)
            – Tamiraa Aquarius
            2 days ago













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          It seems to me that you should put this statement :



          lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";



          outside for loop.



          And in for loop, you use three Int: "Years", "Months", "Days", to represent the accumulation of the time span, but actually they are Int, not Timespan, so after the loop, it could be like: 5 years 20 months 78 days, I assume that is not the kind of result you want.



          Why don't you use TimeSpan type to replace those three Int ?



          Edit:
          You could use the code below:



          // never mind the date of s1 (1990,1,1),it's not important, just as a start point for the Timespan ts1 to do accumulate;
          DateTime s1 = new DateTime(1990, 1, 1);
          TimeSpan ts1 = new TimeSpan();


          for (int i = 0; i < metroGrid1.Rows.Count; ++i)
          {
          ts1 += s1.AddDays(Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value)).AddMonths(Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value)).AddYears(Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value)) - s1;
          }


          int daysResult;
          int monthsResult;
          int yearsResult;


          // get the total number of days, then /365 to get the year count;
          yearsResult = ts1.Days /365;

          // get the month count by /30 from the remainder of /365;
          monthsResult = (ts1.Days % 365) / 30;

          // get the day count from the remainder of /30;
          daysResult = (ts1.Days % 365) % 30;

          lbltotal.Text = "Total: " + yearsResult.ToString() + " years, " + monthsResult.ToString() + " months and " + daysResult.ToString() + " days";





          share|improve this answer














          It seems to me that you should put this statement :



          lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";



          outside for loop.



          And in for loop, you use three Int: "Years", "Months", "Days", to represent the accumulation of the time span, but actually they are Int, not Timespan, so after the loop, it could be like: 5 years 20 months 78 days, I assume that is not the kind of result you want.



          Why don't you use TimeSpan type to replace those three Int ?



          Edit:
          You could use the code below:



          // never mind the date of s1 (1990,1,1),it's not important, just as a start point for the Timespan ts1 to do accumulate;
          DateTime s1 = new DateTime(1990, 1, 1);
          TimeSpan ts1 = new TimeSpan();


          for (int i = 0; i < metroGrid1.Rows.Count; ++i)
          {
          ts1 += s1.AddDays(Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value)).AddMonths(Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value)).AddYears(Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value)) - s1;
          }


          int daysResult;
          int monthsResult;
          int yearsResult;


          // get the total number of days, then /365 to get the year count;
          yearsResult = ts1.Days /365;

          // get the month count by /30 from the remainder of /365;
          monthsResult = (ts1.Days % 365) / 30;

          // get the day count from the remainder of /30;
          daysResult = (ts1.Days % 365) % 30;

          lbltotal.Text = "Total: " + yearsResult.ToString() + " years, " + monthsResult.ToString() + " months and " + daysResult.ToString() + " days";






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          Kuo-hsuan Hsu

          11518




          11518












          • Thanks for updating your answer. It works but it calculates bit different. When I enter start date: 2017.05.01, end date: 2018.10.31 it would be: 1 year, 5 months and 30 days but in label: 1 year 6 months and 3 days. I will try resolve this problem using your code. Thanks for your hard work :)
            – Tamiraa Aquarius
            2 days ago


















          • Thanks for updating your answer. It works but it calculates bit different. When I enter start date: 2017.05.01, end date: 2018.10.31 it would be: 1 year, 5 months and 30 days but in label: 1 year 6 months and 3 days. I will try resolve this problem using your code. Thanks for your hard work :)
            – Tamiraa Aquarius
            2 days ago
















          Thanks for updating your answer. It works but it calculates bit different. When I enter start date: 2017.05.01, end date: 2018.10.31 it would be: 1 year, 5 months and 30 days but in label: 1 year 6 months and 3 days. I will try resolve this problem using your code. Thanks for your hard work :)
          – Tamiraa Aquarius
          2 days ago




          Thanks for updating your answer. It works but it calculates bit different. When I enter start date: 2017.05.01, end date: 2018.10.31 it would be: 1 year, 5 months and 30 days but in label: 1 year 6 months and 3 days. I will try resolve this problem using your code. Thanks for your hard work :)
          – Tamiraa Aquarius
          2 days ago












          up vote
          0
          down vote













          I have tested Kuo-hsuan Hsu solution and it returns:
          Total: 19 years, 8 months and 3 days



          I tried a little bit similar (see below) and get:
          Total: 19 years, 7 months and 26 days
          Windows calculator tells me: 19 Years, 7 Month, 3 weeks, 5 days what is the same as above. But anyway, over a long period my code is not exact and Kuo-hsuan Hsu code isen't it as well.



                 // taken values from grid
          int years = 18 ;
          int months = 18 ;
          int days = 57 ;
          DateTime today = DateTime.Today; // 2018-11-17
          DateTime futuredate = today.AddYears(years);
          futuredate = futuredate.AddMonths(months);
          futuredate = futuredate.AddDays(days); // 2038-07-13

          TimeSpan tsp = futuredate - today;
          years = tsp.Days / 365;
          futuredate = futuredate.AddYears(-years);
          tsp = futuredate - today;
          months = tsp.Days / 30;
          futuredate = futuredate.AddMonths(-months);
          tsp = futuredate - today;
          days = tsp.Days;
          string res1 = "Total: " + years.ToString() + " years, " + months.ToString() + " months and " + days.ToString() + " days";


          I am not happy with the code above.
          I am thinking to run in an loop from start date to end date and calculate the values.
          That would enable me even to count only working days - maybe having a list of public holidays for the next 100 years.






          share|improve this answer























          • Thanks for reply. This one is calculate for past not future :)
            – Tamiraa Aquarius
            2 days ago















          up vote
          0
          down vote













          I have tested Kuo-hsuan Hsu solution and it returns:
          Total: 19 years, 8 months and 3 days



          I tried a little bit similar (see below) and get:
          Total: 19 years, 7 months and 26 days
          Windows calculator tells me: 19 Years, 7 Month, 3 weeks, 5 days what is the same as above. But anyway, over a long period my code is not exact and Kuo-hsuan Hsu code isen't it as well.



                 // taken values from grid
          int years = 18 ;
          int months = 18 ;
          int days = 57 ;
          DateTime today = DateTime.Today; // 2018-11-17
          DateTime futuredate = today.AddYears(years);
          futuredate = futuredate.AddMonths(months);
          futuredate = futuredate.AddDays(days); // 2038-07-13

          TimeSpan tsp = futuredate - today;
          years = tsp.Days / 365;
          futuredate = futuredate.AddYears(-years);
          tsp = futuredate - today;
          months = tsp.Days / 30;
          futuredate = futuredate.AddMonths(-months);
          tsp = futuredate - today;
          days = tsp.Days;
          string res1 = "Total: " + years.ToString() + " years, " + months.ToString() + " months and " + days.ToString() + " days";


          I am not happy with the code above.
          I am thinking to run in an loop from start date to end date and calculate the values.
          That would enable me even to count only working days - maybe having a list of public holidays for the next 100 years.






          share|improve this answer























          • Thanks for reply. This one is calculate for past not future :)
            – Tamiraa Aquarius
            2 days ago













          up vote
          0
          down vote










          up vote
          0
          down vote









          I have tested Kuo-hsuan Hsu solution and it returns:
          Total: 19 years, 8 months and 3 days



          I tried a little bit similar (see below) and get:
          Total: 19 years, 7 months and 26 days
          Windows calculator tells me: 19 Years, 7 Month, 3 weeks, 5 days what is the same as above. But anyway, over a long period my code is not exact and Kuo-hsuan Hsu code isen't it as well.



                 // taken values from grid
          int years = 18 ;
          int months = 18 ;
          int days = 57 ;
          DateTime today = DateTime.Today; // 2018-11-17
          DateTime futuredate = today.AddYears(years);
          futuredate = futuredate.AddMonths(months);
          futuredate = futuredate.AddDays(days); // 2038-07-13

          TimeSpan tsp = futuredate - today;
          years = tsp.Days / 365;
          futuredate = futuredate.AddYears(-years);
          tsp = futuredate - today;
          months = tsp.Days / 30;
          futuredate = futuredate.AddMonths(-months);
          tsp = futuredate - today;
          days = tsp.Days;
          string res1 = "Total: " + years.ToString() + " years, " + months.ToString() + " months and " + days.ToString() + " days";


          I am not happy with the code above.
          I am thinking to run in an loop from start date to end date and calculate the values.
          That would enable me even to count only working days - maybe having a list of public holidays for the next 100 years.






          share|improve this answer














          I have tested Kuo-hsuan Hsu solution and it returns:
          Total: 19 years, 8 months and 3 days



          I tried a little bit similar (see below) and get:
          Total: 19 years, 7 months and 26 days
          Windows calculator tells me: 19 Years, 7 Month, 3 weeks, 5 days what is the same as above. But anyway, over a long period my code is not exact and Kuo-hsuan Hsu code isen't it as well.



                 // taken values from grid
          int years = 18 ;
          int months = 18 ;
          int days = 57 ;
          DateTime today = DateTime.Today; // 2018-11-17
          DateTime futuredate = today.AddYears(years);
          futuredate = futuredate.AddMonths(months);
          futuredate = futuredate.AddDays(days); // 2038-07-13

          TimeSpan tsp = futuredate - today;
          years = tsp.Days / 365;
          futuredate = futuredate.AddYears(-years);
          tsp = futuredate - today;
          months = tsp.Days / 30;
          futuredate = futuredate.AddMonths(-months);
          tsp = futuredate - today;
          days = tsp.Days;
          string res1 = "Total: " + years.ToString() + " years, " + months.ToString() + " months and " + days.ToString() + " days";


          I am not happy with the code above.
          I am thinking to run in an loop from start date to end date and calculate the values.
          That would enable me even to count only working days - maybe having a list of public holidays for the next 100 years.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          Wilhelm

          364




          364












          • Thanks for reply. This one is calculate for past not future :)
            – Tamiraa Aquarius
            2 days ago


















          • Thanks for reply. This one is calculate for past not future :)
            – Tamiraa Aquarius
            2 days ago
















          Thanks for reply. This one is calculate for past not future :)
          – Tamiraa Aquarius
          2 days ago




          Thanks for reply. This one is calculate for past not future :)
          – Tamiraa Aquarius
          2 days ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350250%2fwinform-calculate-all-gridview-to-date%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin