1

I have a filter to get records that falls between a period of time e.i 7 days ago or 90 days ago now ... I would like to add year quarters to my method, here is my current code, if you have a great way to archive this, please share, many thanks!

DateTime Result = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
switch (days)
{
    case -1:
    {
        Result = new DateTime(Result.Year, Result.Month, 1);
        break;
    }
    case -2:
    {
        Result = new DateTime(Result.Year, Result.Month, 1);
        Result = Result.AddMonths(-1);
        break;
    }
    case -3:
    {
        Result = new DateTime(Result.Year, 1, 1);
        break;
    }
    case -4:
    {
        Result = new DateTime(Result.Year - 1, 1, 1);
        break;
    }
    case 1:
    case 7:
    case 30:
    case 60:
    case 90:
    case 120:
    {
        Result = Result.AddDays(-days);
        break;
    }
}
return Result;

If this also helps this is my Html select

<select id="data">
  <option value="-5" selected="selected">All</option>
  <option value="0">Today</option>
  <option value="1">Yesterday</option>
  <option value="7">Last 7 Days</option>
  <option value="30">Last 30 Days</option>
  <option value="60">Last 60 Days</option>
  <option value="90">Last 90 Days</option>
  <option value="120">Last 120 Days</option>
  <option value="-1">This Month</option>
  <option value="-2">Last and Current Month</option>
  <option value="-3">Current Year</option>
  <option value="-4">Last and Current Year</option>
</select>
Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
papapi
  • 61
  • 9
  • Take a look at this, https://stackoverflow.com/a/8698345/2348184 – Jeff Anderson Oct 03 '18 at 18:30
  • @JeffAnderson Thank for your reply, I have already looked at that, unfortunately it does not answer the current scenario – papapi Oct 03 '18 at 18:33
  • 1
    can you clarify, what is your desired input/output... are you looking for "first quarter", "2nd quarter" etc? of the current year? or something else – Sichi Oct 03 '18 at 18:42
  • Hi @Sichi I want to select Q1 (or Q2,Q3,Q4) and get the Quaters period ( start - & - end ) in days – papapi Oct 03 '18 at 18:46
  • 1
    Are these static quarters IE, Jan-March, Apr-Jun ect or dynamic based of the date returned by Now()? – Sorceri Oct 03 '18 at 20:18
  • Code style comment: couldn't you make your values meaningful, like "today", "yesterday", "last7", "last30", etc? Expecting some programmer (yourself six months from now included) to understand that "-2" means "Last and Current Month" is making his job harder. That's like using `f6` in place of a perfectly meaningful variable name like `totalThisYear`. – Jim Mischel Oct 04 '18 at 03:39
  • You probably need to rewrite your function so that it returns the start date and the end date. Or, use this function to get the starting date and create a new function that returns the ending date. – Jim Mischel Oct 04 '18 at 03:42
  • @JimMischel yes yes, Its a personal project and I'm the only one who will ever work on it, thank you for your advice.. much appreciated – papapi Oct 04 '18 at 08:42
  • @JimMischel I've been pulling my beard out for a few hours now.. – papapi Oct 04 '18 at 08:43
  • Well, okay. But remember that the things you practice are the things that become habit. Also, I can tell you from experience that private code, stuff you think that "nobody else will ever see", has a distressing tendency to become public code. As for "pulling your beard out," what is the specific problem you have with returning the ending date? And what's the answer to the question by @Sorceri? – Jim Mischel Oct 04 '18 at 14:42

0 Answers0