charlie_cat
charlie_cat

Reputation: 1850

Not sure what type it should be

I want to extract all the sundays in the current month and have this code:

 private string GetDatesOfSundays(DateTime DatMonth)
 {
  string sReturn = "";
  int iDayOffset = DatMonth.Day - 1;
  DatMonth = DatMonth.AddDays(System.Convert.ToDouble(-DatMonth.Day + 1));
  DateTime DatMonth2 = DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));
  while (DatMonth < DatMonth2)
  {
    if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
    {
      if (sReturn.Length > 0) sReturn += ",";
      sReturn += DatMonth.ToShortDateString();
    }
    DatMonth = DatMonth.AddDays(1.0);
  }
  return sReturn;
}   

[HttpGet]
public ActionResult TradeUKKPISearchesData()
{
  string allSundaysInMonth = GetDatesOfSundays(System.DateTime.Now);  
  //var reportData = _reportingService.GetTradeUKKPISearches();
  //return View(reportData);
}

the problem lies with my type string for allSundaysInMonth and is also empty ofcourse. The sReturn is of type string but then again I pass a date(I know :) ) but what type should allSundaysInMonth be? sReturn does have the correct dates in...I need to display these dates in a dropdown in the view of the controller so the user can select any of the sundays for which he/she needs to run a report for.

thanks

Upvotes: 0

Views: 113

Answers (2)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48560

You can go for DateTime[] or IEnumerable<DateTime>.

Your method signature should be

private IEnumerable<DateTime> GetDatesOfSundays(DateTime DatMonth)

or

private DateTime[] GetDatesOfSundays(DateTime DatMonth)

If you havn't worked with IEnumerable go for this

private DateTime[] GetDatesOfSundays(DateTime DatMonth)
{
    List<DateTime> lst = new List<DateTime>();

    DatMonth = DatMonth.AddDays(-DatMonth.Day + 1);
    DateTime DatMonth2 = DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));

    while (DatMonth < DatMonth2)
    {
        if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
        {
            lst.Add(DatMonth);
            DatMonth = DatMonth.AddDays(7);
            continue;
        }

        DatMonth = DatMonth.AddDays(1);
    }

    return lst.ToArray();
}

and call it as

DateTime[] allSundaysInMonth = GetDatesOfSundays(System.DateTime.Now);

Upvotes: 0

Jodrell
Jodrell

Reputation: 35726

How about

private IEnumerable<DateTime> GetDatesOfSundays(DateTime DatMonth)
{
    int iDayOffset = DatMonth.Day - 1;   
    DatMonth = DatMonth.AddDays(System.Convert.ToDouble(-DatMonth.Day + 1));
    DateTime DatMonth2 =
        DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));
    while (DatMonth < DatMonth2)
    {
        if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
        {
            yield return DatMonth;
        }

        DatMonth = DatMonth.AddDays(1.0);
    }
}

I would be tempted to rewrite your function as an extension somthing like this

public static IEnumerable<Datetime> DaysOfMonth(
    this DateTime any,
    DayOfWeek day)
{
    // start at first of month
    var candidate = new DateTime(any.Year, any.Month, 1);

    var offset = (int)day - (int)candidate.DayOfWeek;

    if (offset < 0)
    {
        offset += 7
    }

    candidate = candidate.AddDays(offset);

    while (cadidate.Month == any.Month)
    {
        yield return candidate;
        candidate = candidate.AddDays(7.0)
    }
}

Then you could use it like this

var allSundaysInMonth = DateTime.Now.DaysOfMonth(DayOfWeek.Sunday);

If you want to convert an IEnumerable<DateTime> to a string you could do this,

var listOfDates = string.Join<DateTime>(", ", allSundaysInMonth);

using this string.Join overload


If you really want the days as a DateTime[] you could do this (but there is no need)

DateTime[] allSundaysInMonth = GetDatesOfSundays(DateTime.Now).ToArray();

or for my extension example

var allSundaysInMonth = DateTime.Now.DaysOfMonth(DayOfWeek.Sunday).ToArray();

Upvotes: 5

Related Questions