jaekie
jaekie

Reputation: 2303

Getting number of months based on days in c#

I'm stuck trying to figure this one out..

We currently have a date criteria on our reports, that are limited by days, configurable of course, currently set to 90 days.. message says, it is limited by 90 days, however my boss wants to increase it to 13 months, unfortunately if I did that, I'd need to do it by days and it would say, 395 days..

Not a very friendly message..

Trying to figure out a way to satisfy this, my other only option is to add another settings that is limited by months as well as days. but then i still need to convert the months back to days which wont be perfect since not every month has same days..

Ideas?

Upvotes: 1

Views: 2281

Answers (4)

YetAnotherUser
YetAnotherUser

Reputation: 9356

TimeSpan give you duration between two DateTime objects. It can give it consistently in Days, Hours or Mins; Number of months would be different based upon actual start & end dates as different months have different number of actual days.

Having said that, you can always write a Utility method that gives you YourTimeSpan object that gives you number of Months etc based upon your calendar and StartDate / EndDates.

In your case you can make it even simpler by storing it separately in configuration, for example - ReportDuration_Years, ReportDuration_Months, ReportDuration_Days. This would allow you to create meaningful lable on your report as well as allow to identify StartDate and EndDate properly.

//Call this by passing values from configuration
private string GetNiceLookingLable(int? years, int? months, int? days){
    var yearMessage = (years.HasValue)?String.Format("{0} Years", years):String.Empty;
    var monthMessage = (months.HasValue)?String.Format("{0} Months", months):String.Empty;
    var daysMessage = (days.HasValue)?String.Format("{0} Days", days):String.Empty;

    // You probably want to concatenate them properly
    return String.Format("{0} {1} {2}",yearMessage, monthMessage, daysMessage);
}

-

//Call this to get starting date
private DateTime getStartingDate(int? years, int? months,int? days){
     var retDate = DateTime.Today;
     if(years.HasValue){
         retDate = retDate.AddYears(-1*years.Value);
     }
     if(months.HasValue){
         retDate = retDate.AddMonths(-1*months.Value);
     }
     if(days.HasValue){
         retDate = retDate.AddDays(-1*days.Value);
     }

     return retDate;
}

Upvotes: 0

Steve Morgan
Steve Morgan

Reputation: 13091

You need to decide if you're going to use 13 months as the time interval, or some number of days that approximates to 13 months. If you use 13 months, then the number of days (or the end date for your report) is going to vary depending on the start date.

I would suggest making your report configurable for either months or days (storing not just the number, but the units in configuration). You can then display on the report whatever has been specified in the configuration (with the units from configuration, too) and calculate the end date for the query by adding the configured number of configured units to the start date.

If you try to do everything in days, when you're now working in months, you'll just make life difficult for yourself.

It's much easier to add 13 months to the start date to get the end date, than it is to try and (inaccurately) work out how many months in a given number of days.

Upvotes: 3

Ed Bayiates
Ed Bayiates

Reputation: 11230

I would do something like this, given the number of days:

int myDays;   // 390 or whatever
DateTime d1 = DateTime.Now;
DateTime d2 = d1.AddDays(myDays);
int monthsDiff = d2.Month - d1.Month + 12 * (d2.Year - d1.Year);
DateTime d3 = d1.AddMonths(monthsDiff);
TimeSpan tf = d2 - d3;
string msg = monthsDiff.ToString() + " months, " + tf.Days + " days";

Upvotes: 0

TheGeekYouNeed
TheGeekYouNeed

Reputation: 7539

Use the TimeSpan object to perform the calculations you need for your date criteria.

Upvotes: 1

Related Questions