-
Nov 11th, 2020, 09:06 AM
#1
Thread Starter
Member
[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();
}
}
-
Nov 11th, 2020, 07:11 PM
#2
Re: Calculating remaining days between two dates
vb.net Code:
Dim daysUntilDue = (dueDate - Date.Today).Days
Subtracting one Date from another produces a TimeSpan and that has a Days property.
-
Nov 11th, 2020, 10:56 PM
#3
Member
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 >
-
Nov 11th, 2020, 11:19 PM
#4
Re: Calculating remaining days between two dates
Originally Posted by Sherin
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|