Question

I would like to get the nearest based from a list of days:

Scenario 1:

Date: July 22, 2013 (Monday)
Possible days: "Tuesday", "Wednesday", "Friday" (string values)
Answer: July 23, 2013 (Tuesday)

Scenario 2:

Date: July 23, 2013 (Tuesday)
Possible days: "Tuesday", "Wednesday", "Thursday", "Saturday"
Answer: July 24, 2013 (Wednesday)

Scenario 3:

Date: July 24, 2013 (Wednesday)
Possible days: "Monday", "Tuesday"
Answer: July 29, 2013 (Monday)

Any suggestions?

Was it helpful?

Solution

DateTime date = DateTime.Parse("July 22, 2013");
        DayOfWeek dateDay = date.DayOfWeek;
        DayOfWeek[] possibleDays = { DayOfWeek.Tuesday,DayOfWeek.Wednesday,DayOfWeek.Friday };
        int addToBestAnswer = 7;
        foreach (var checkDay in possibleDays)
        {
            if (checkDay-dateDay<addToBestAnswer)
            {
                addToBestAnswer = checkDay - dateDay;
            }
        }
        DateTime Answer = date.AddDays(addToBestAnswer);

Edit: for only string input:

DateTime date = DateTime.Parse("July 22, 2013");
        string[] possibleDays={ "Tuesday","Wednesday","Friday" };

        List<int> pDays = new List<int>();
        foreach (var inputDay in possibleDays)
        {
            pDays.Add(int.Parse(inputDay.Replace("Sunday", "0").Replace("Monday", "1").Replace("Tuesday", "2").Replace("Wednesday", "3").Replace("Thursday", "4").Replace("Friday", "5").Replace("Saturday", "6")));
        }
        int dateDay = (int)date.DayOfWeek;

        int addToBestAnswer = 7;
        foreach (var checkDay in pDays)
        {
            int difference = checkDay - dateDay;
            if (difference<0)
            {
                difference = 7 + difference;
            }
            if (difference<addToBestAnswer&&difference!=0)
            {
                addToBestAnswer = difference;
            }
        }
        DateTime Answer = date.AddDays(addToBestAnswer);
        // Answer.ToShortDateString()+" ("+Answer.DayOfWeek.ToString()+")";

OTHER TIPS

Like this ?

    public DateTime GetNextPossibleDay(DateTime DT, DayOfWeek[] PossibleDays)
    {
        if (PossibleDays.Length == 0)
            throw new Exception("No possible day.");

        do
        {
            DT = DT.AddDays(1);
        }
        while (!PossibleDays.Contains(DT.DayOfWeek));

        return DT;
    }

You can check every date in the list, unless you get correct one, something like this:

   var days = new List<string> {"Tuesday", "Monday"};
   var startDate = new DateTime(2013, 7, 24).AddDays(1);
   while (!days.Contains(startDate.DayOfWeek.ToString("G")))
   {
        startDate = startDate.AddDays(1);
   }
   Console.WriteLine(startDate);

Tested. This code works

  List<DayOfWeek> days = new List<DayOfWeek>() { DayOfWeek.Tuesday, DayOfWeek.Wednesday };
        DateTime sourceDate = DateTime.Now;
        DayOfWeek currentDay = sourceDate.DayOfWeek;

        int? smallestValue = null;

        foreach (DayOfWeek d in days)
        {
            int currentValue = (int)d - (int)currentDay;
            if (!smallestValue.HasValue)
                smallestValue = currentValue;

            if(smallestValue > currentValue)
                smallestValue = currentValue;

        }

        DateTime nearestDate = sourceDate.AddDays(smallestValue.Value);

Not a fancy Linq, but this works =)

    public static DateTime NearestDate(DateTime baseDateTime, List<string> acceptedDays)
    {
        DateTime result = new DateTime(baseDateTime.Year, baseDateTime.Month, baseDateTime.Day);

        List<DayOfWeek> acceptedDoW = new List<DayOfWeek>();
        acceptedDays.ForEach(x => acceptedDoW.Add((DayOfWeek)Enum.Parse(typeof(DayOfWeek), x, true)));

        DayOfWeek currentDay = baseDateTime.DayOfWeek;

        int closestDay = int.MaxValue;

        acceptedDoW.ForEach(x =>
            {
                int currentSpan = (int)x;

                if (x < currentDay)
                    currentSpan += 7;

                currentSpan = currentSpan - (int)currentDay;

                if (currentSpan < closestDay)
                    closestDay = currentSpan;
            });

        return result.AddDays(closestDay);
    }

This should work:

var date = new DateTime(2013, 07, 24);
var posibleDays = new[]{DayOfWeek.Tuesday,DayOfWeek.Wednesday,DayOfWeek.Friday};

var nearestDay = posibleDays.Select(dow => new { 
    DayOfWeek = dow, 
    Diff = (7 + (dow - date.DayOfWeek)) % 7 
})
.Where(x => x.Diff >= 1)
.OrderBy(x => x.Diff)
.FirstOrDefault();

Inspired by a question of myself some time ago. It assumes a day to be in the next week if the DayOfWeek value is earlier than another.

Here a demo with your sample data: http://ideone.com/VzZnzx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top