Subtrating two dates to calculate number of days
how to subtract two datetime without subtracting time?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 16, 2013, 12:30 AM EDT
difference.TotalDayswill give you number of daysvar days = difference.TotalDays;VulpesRank #55 · 29.2K reputationPosted Jul 16, 2013, 12:56 AM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Jul 16, 2013, 1:10 AM EDT
hi,
Are you looking in SQL or C#?
if it is in C# than agree with above answer
if you look in SQL than try
select DATEDIFF(d,'2013-05-20 12:12:45','2013-07-20 15:12:45')
hope this will help you.
Nahul Antony25 reputationPosted Jul 16, 2013, 1:17 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 16, 2013, 1:20 AM EDT
Can you provide your example?
Regards,
Iftikar
Nahul Antony25 reputationPosted Jul 16, 2013, 1:21 AM EDT
Nahul Antony25 reputationPosted Jul 16, 2013, 1:24 AM EDT
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 16, 2013, 1:25 AM EDT
DateTime d1=Convert.ToDateTime("12/22/2013");
DateTime d2 = Convert.ToDateTime("12/22/2012");
var d = (d1 - d2).TotalDays;
output:365
Nahul Antony25 reputationPosted Jul 16, 2013, 1:26 AM EDT
double s1;
double s2;
DateTime dt = System.DateTime.Today;
DateTime dt1 = dtpDate.Value;
ts = (dt - dt1);
s1 = Convert.ToDouble(ts.TotalDays);
//s2 = Convert.ToDouble(ts.TotalMinutes);
txtTotDays.Text = Convert.ToString(s1);
i not need to subtract time.i need date to subtract from datetime
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 16, 2013, 1:36 AM EDT
your code is giving the correct answer. no or days between current day and input day.
so what the problem you are getting.
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 16, 2013, 1:45 AM EDT
Regards,
Iftikar