On my form I have a monthCalendar with which I enter an EndDate (when something needs to happen) into a database. The time when I enter is now. So I have 2 values. Now and EndDate.
How to calculate how many days, months, years is between these two values? I would like to put the 3rd value (the difference between 1st and 2nd) into a textBox!
I am a rookie at C#, so please don`t be mad if my questions goona sound stupid... | | Mitja Bonca Wednesday, June 10, 2009 9:17 AM |
DateTime dt1 = DateTime.Now;
DateTime dt2 = <EndDate>
DateTime dtSpan = new DateTime();
TimeSpan span = new TimeSpan();
span = dt2.Subtract(dt1);
double numDays = span.TotalDays;
This would give you number of days. And, so you can carry on.
Regards,
Lakra :)
- If the post is helpful or answers your question, please mark it as such. - Marked As Answer byLinda LiuMSFT, ModeratorFriday, June 19, 2009 10:44 AM
-
| | Abhijeet Lakra Wednesday, June 10, 2009 9:30 AM | One more valuable point of advice: Suppose, nowDateTime = 12-Apr-2008 10:00:00 AM and EndDateTime = 14-Apr-2008 08:00:00 AM
In this case, how many number of days difference do you expect as answer for your Application? If you expect 2 days, well the output by C# would be 1 day because it also considers time(HH:MM:SS) into account. So, what you can do is build your own DateTime value before processing, something as:
DateTime dtNow = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
dt2 = new DateTime(dt2.Year, dt2.Month, dt2.Day);
This would have the common default time for both the dateTime values.
Regards,
Lakra :)
- If the post is helpful or answers your question, please mark it as such. - Marked As Answer byLinda LiuMSFT, ModeratorFriday, June 19, 2009 10:45 AM
-
| | Abhijeet Lakra Wednesday, June 10, 2009 9:50 AM | You can use DateTime.Now.Subtract to get another datetime object.
Thanks,
A.m.a.L
.Net Goodies
|
|
|
Remember to click "mark as answered" when you get a correct reply to your question
|
| | A.m.a.L - aditi.com - Think Product Wednesday, June 10, 2009 9:25 AM |
Hello mitja, By calculating TimeSpan you can get year,months.. try like this,
DateTime dtNow;
DateTime dtEnd;
TimeSpan ts = dtEnd.Subtract(dtNow);
int year= ts.Days / 365; int weeks = ts.Days/7;
| | NareshG Wednesday, June 10, 2009 9:27 AM |
DateTime dt1 = DateTime.Now;
DateTime dt2 = <EndDate>
DateTime dtSpan = new DateTime();
TimeSpan span = new TimeSpan();
span = dt2.Subtract(dt1);
double numDays = span.TotalDays;
This would give you number of days. And, so you can carry on.
Regards,
Lakra :)
- If the post is helpful or answers your question, please mark it as such. - Marked As Answer byLinda LiuMSFT, ModeratorFriday, June 19, 2009 10:44 AM
-
| | Abhijeet Lakra Wednesday, June 10, 2009 9:30 AM | One more valuable point of advice: Suppose, nowDateTime = 12-Apr-2008 10:00:00 AM and EndDateTime = 14-Apr-2008 08:00:00 AM
In this case, how many number of days difference do you expect as answer for your Application? If you expect 2 days, well the output by C# would be 1 day because it also considers time(HH:MM:SS) into account. So, what you can do is build your own DateTime value before processing, something as:
DateTime dtNow = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
dt2 = new DateTime(dt2.Year, dt2.Month, dt2.Day);
This would have the common default time for both the dateTime values.
Regards,
Lakra :)
- If the post is helpful or answers your question, please mark it as such. - Marked As Answer byLinda LiuMSFT, ModeratorFriday, June 19, 2009 10:45 AM
-
| | Abhijeet Lakra Wednesday, June 10, 2009 9:50 AM | Yes, the lowest value for time I want to be days. And thx for the advice Abhijeet! Will take this into account. I am a rookie at C#, so please don`t be mad if my questions goona sound stupid... | | Mitja Bonca Wednesday, June 10, 2009 1:59 PM |
|