Results 1 to 4 of 4

Thread: [RESOLVED] Calculating remaining days between two dates

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Resolved [RESOLVED] Calculating remaining days between two dates

    I have used Persian calendar in my project. The date is selected using a component date picker (a reference dll file). By pressing the add button, all input information including Persian date is added to the DataGridView cells. An example has been shown the image below. I want to calculate the remaining days between due date (second column from left) and current date, then put the result in the last column (first column from right). Please help me.
    The button code is:
    Code:
     private void btnAddNew_Click(object sender, EventArgs e)
            {
                if (txtboxID.Text != "" || txtboxCostCenter.Text != "" || txtboxReceiver.Text != "" || txtboxWorkCenter.Text != "")
                {
                    string StartTime = dateTimePickerX1.GetSelectedDateInPersianDateTime().ToShortDateString();
                    string EndTime = dateTimePickerX2.GetSelectedDateInPersianDateTime().ToShortDateString();
                    dataGridView1.Rows.Add(txtboxID.Text, txtboxCostCenter.Text, comBoxPtype.Text, comBoxServiceType.Text, txtboxReceiver.Text, txtboxWorkCenter.Text,StartTime, EndTime);
                    iReset();
                }
            }
    Name:  Untitled.jpg
Views: 50
Size:  21.6 KB

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,061

    Re: Calculating remaining days between two dates

    vb.net Code:
    1. Dim daysUntilDue = (dueDate - Date.Today).Days
    Subtracting one Date from another produces a TimeSpan and that has a Days property.

  3. #3
    Member
    Join Date
    Jul 2019
    Location
    Ahmedabad
    Posts
    45

    Re: Calculating remaining days between two dates

    Hello,@fafa2020

    Please try this code, To Calculating remaining days between two dates

    Assuming the StartingDate and EndingDate are of type DateTime.

    Code:
    (EndingDate - StartingDate).TotalDays
    I hope this code will be useful for you.
    Thank you.
    < advertising removed by moderator >

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,061

    Re: Calculating remaining days between two dates

    Quote Originally Posted by Sherin View Post
    Hello,@fafa2020

    Please try this code, To Calculating remaining days between two dates

    Assuming the StartingDate and EndingDate are of type DateTime.

    Code:
    (EndingDate - StartingDate).TotalDays
    I hope this code will be useful for you.
    Thank you.
    TotalDays is likely less appropriate than Days. For one thing, if the time portions of the values is zeroed then both properties will return the same number, but TotalDays will return it as a Double and Days as an Integer. If you're comparing whole numbers then you should be using Integers.

    Secondly, if the time portions are not zeroed but you're only interested in whole days, which seems likely, the TotalDays could end up giving you the wrong result. For instance, if you were to check whether the result was less than or equal to 10 and you were assuming that the next value outside that range was 11 but TotalDays returned 10.5, your code would not behave as you expected.

    That said, if you are specifically interested in partial days then TotalDays would be more appropriate. In that case, you'd need to change Date.Today in my code to Date.Now as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width