I have a date in my SQL Server database, I want to calculate how many dates on that day to today date. Imagine I create an invoice in 5/31/2019
and it saves in database and today (6/5/2019)
I'm going a create payment receipt for it, now I want to calculate the date period.
Asked
Active
Viewed 192 times
-1

marc_s
- 732,580
- 175
- 1,330
- 1,459

Shani Rathnayake
- 1
- 3
-
no i wanna get one date from data base and other date is today date.. – Shani Rathnayake Jun 05 '19 at 05:22
-
@ShaniRathnayake When u say u want to calculate 'date period', what does that mean? can you show your expected output for the scenario that you have mentioned in your question? – akg179 Jun 05 '19 at 05:30
-
@akg179 when (5/31/2019) - (6/5/2019) the answer should be 5 days – Shani Rathnayake Jun 05 '19 at 05:34
-
ok guys thanks for help me. i found answer. – Shani Rathnayake Jun 05 '19 at 06:01
-
while (Dr2.Read()) { textBox1.Text = Dr2["Date"].ToString(); DateTime s1 = DateTime.Parse(TxtReceiptD.Text); DateTime s2 = DateTime.Parse(Dr2["Date"].ToString()); TimeSpan T = s1 - s2; int days = (int)T.TotalDays; TxtMonthIntAmt.Text = days.ToString(); } – Shani Rathnayake Jun 05 '19 at 06:01
-
@ShaniRathnayake Here you need do `int days = (s2 - s1).Days` – Prasad Telkikar Jun 05 '19 at 06:36
3 Answers
3
As example:
DateTime StartDate = DateTime.Now.AddDays(-2);
DateTime EndDate = DateTime.Now;
double days = (EndDate - StartDate).TotalDays;
Debug.WriteLine(days);

Prashant Pimpale
- 10,349
- 9
- 44
- 84

Marvin Schuchardt
- 123
- 1
- 9
-
OP is trying to get int where he need difference between only `Date` – Prasad Telkikar Jun 05 '19 at 09:45
1
If you have two DateTime
and you want to calculate in between these two dates, the you can substract Dates and get Days
out of it
Something like,
DateTime ReceiptDate = new DateTime(2019, 05, 31)
int difference = (DateTime.Now - ReceiptDate).Days //ReceiptDate is type of DateTime which is converted from database value
Console.WriteLine(difference);
Output:
5
POC : .net Fiddle

Prasad Telkikar
- 15,207
- 5
- 21
- 44
0
The only way I could think of would be to assign each day a numeric value, making sure to account for leap years. For instance, January 8th would be 8, as it's the 8th day of the year. February 8th would be 39. And to calculate how many days had passed, just subtract the first date from the second. So from Jan 8 to Feb 8 is (39-8) = 31 days.

MegaEmailman
- 505
- 3
- 11
-
-
2Wait, you want someone to actually write it for you? Sorry, I thought you were just wondering how you would go about it. – MegaEmailman Jun 05 '19 at 05:17