How can i get year, month different from calendar?
-
If user click dates such as 2008-06-12 ~ 2011-09-25 I want to get year, month different not days. example) 3years 2month I am using TimeSpan function. but this function only show day's difference.
hi My english is a little. anyway, nice to meet you~~ and give me your advice anytime~
-
If user click dates such as 2008-06-12 ~ 2011-09-25 I want to get year, month different not days. example) 3years 2month I am using TimeSpan function. but this function only show day's difference.
hi My english is a little. anyway, nice to meet you~~ and give me your advice anytime~
How long is a year? As you found, TimeSpan won't/can't do that, but you can probably do it manually.
-
If user click dates such as 2008-06-12 ~ 2011-09-25 I want to get year, month different not days. example) 3years 2month I am using TimeSpan function. but this function only show day's difference.
hi My english is a little. anyway, nice to meet you~~ and give me your advice anytime~
It depends on what you really want. For example 3 Years and 2 months could be between 1154 and 1158 days depending on which months you're actually talking about and if a leap year was included. But I think for what you want you're going to have to just check the difference between the years/months/days yourself, I reckon something like this:
public struct Diff
{
public int Years;
public int Months;
public int Days;
};//Will return incorrect negative values if end < start
public Diff Difference(DateTime start, DateTime end)
{
Diff diff = new Diff();//Not as far into month, so it doesn't count
if (end.Day < start.Day)
{
end = end.AddMonths(-1);//Calc diff int remainDays = DateTime.DaysInMonth(start.Year, start.Month) - start.Day; diff.Days = remainDays + end.Day;
}
else
diff.Days = end.Day - start.Day;//Not as far into year, so it doesn't count
if (end.Month < start.Month)
{
end = end.AddYears(-1);//Calc diff diff.Months = 12 - (start.Month - end.Month);
}
else
diff.Months = end.Month - start.Month;diff.Years = end.Year - start.Year;
return diff;
}Which puts your example at 3 years and 3 months (and 13 days) since your further into September than you are into June, if you used 2008-06-12 ~ 2011-09-8 it would give 3 years 2 months 26 days. Which may or may not be quite what you want, but I think you get the idea. Edit: Fixed function, mustn't write code when sleepy
-SK Genius
Vehicle Simulation Demo - New and Improved!