umbraco-community / Our.Umbraco.OpeningHours

An Umbraco property editor for handling opening hours
MIT License
9 stars 13 forks source link

Add method to get next openings hours set #21

Open bjarnef opened 7 years ago

bjarnef commented 7 years ago

I would be useful to have an method to get next opening hours, e.g. GetNextOpeningHours() Maybe with the option to choose whether it should take today into account - maybe with the change of https://github.com/bomortensen/Our.Umbraco.OpeningHours/issues/15

For now I do like this:

var nextDays = openingHours.GetUpcomingDays(7);
var nextDayOpen = nextDays.Where(x => x.IsOpen && !x.IsToday).FirstOrDefault();
<p>We are opening again on @nextDayOpen.WeekDayName at @nextDayOpen.Items[0].Opens.ToString(@"HH\:mm")</p>
fret1224 commented 7 years ago

I found that I needed this functionality too. I did want it to take today into account. My version is a little longer because of that and some differences with the text that my version prints (today, tomorrow, or day of week). Anyway, here's the snippet incase it helps anyone.

foreach(OpeningHoursDay day in openingHours.GetUpcomingDays(7))
{
    string dayText = "";
    string timeText = "";

    if(day.IsOpen){
        dayText = day.WeekDayName;
        if(day.IsToday){ dayText = "today"; }
        else if(day.IsTomorrow){ dayText = "tomorrow"; }

        foreach(OpeningHoursDayTimeSlot item in day.Items)
        {
            if(item.Opens < DateTime.Now){ continue; }

            // Dump minutes if they equal "00", update format of am/pm to be lowercase include periods (AP style).
            timeText = item.Opens.ToString("h:mm tt").ToLower().Replace(":00", "").Replace("m", ".m.");
        }
    }

    // If timeText is empty we'll just go to the next day. Otherwise print the result and break out of the foreach.
    if(timeText != ""){
        <p class="estimated">Re-opening @dayText at @timeText</p>
        break;
    }
}