-1

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.

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

3 Answers3

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
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