S

#### shapper

I am trying to get the span between two dates in seconds, minutes,

hours, weeks, months or years.

I came up with the following:

public static Int64 Span(DateTime begin, DateTime end,

TimeInterval interval) {

// Check inputs

if (begin > end) throw new ArgumentException("Begin date must be

previous or equal to end date");

// Define count

TimeSpan span = end - begin;

switch (interval) {

case TimeInterval.Year:

return (Int64)(end.Year - begin.Year);

case TimeInterval.Month:

return (Int64)((end.Month - begin.Month) + (12 * (end.Year -

begin.Year)));

case TimeInterval.Week:

return (Int64)Math.Floor(span.TotalDays) / 7;

case TimeInterval.Day:

return (Int64)Math.Floor(span.TotalDays);

case TimeInterval.Hour:

return (Int64)Math.Floor(span.TotalHours);

case TimeInterval.Minute:

return (Int64)Math.Floor(span.TotalMinutes);

case TimeInterval.Second:

return (Int64)Math.Floor(span.TotalSeconds);

default:

return (Int64)span.Ticks;

}

} // Span

For example, in months, I was trying to get the following rule:

The difference between 2010-20-01 and 2010-18-02 is 0 months.

The difference between 2010-20-01 and 2010-20-02 is 1 month.

So it becomes only 1 month if the day of the end date is after the day

of the being date.

Does this make sense? How can I do this?

I am not sure if I can make other improvements to my code ... But any

suggestion is welcome.

Thank You,

Miguel