显示下一个营业日和营业时间

时间:2018-07-18 17:43:41

标签: c# razor

在我的申请中,我正在等待办公室的开放和关闭时间。 这样的例子

Monday    02:30PM  - 05:00PM
Tuesday   Closed
Wednesday 09:00AM  - 05:00PM
Thursday  Closed
Friday    Closed
Saturday  Closed
Sunday    Closed

办公室也可以每周开放一次,时间会有所不同。

Monday    02:30PM  - 05:00PM
Tuesday   Closed
Wednesday Closed
Thursday  Closed
Friday    Closed
Saturday  Closed
Sunday    Closed

下面的代码仅显示与上述时间相同的时间。

我的问题是,是星期四,星期五,星期六还是星期日,我想显示办公室将在星期一的02:30 PM营业。如果在星期二,它将显示它将在星期三上午9点开放。

我如何计算以显示下一个办公日和营业时间

@foreach (var weekday in Model.OfficeHours.WeekdayHours)
{
    @weekday.DayName <br>
    @weekday.OpenTime.ToString("hh:mmtt")<br>
    @weekday.CloseTime.ToString("hh:mmtt")
}

public IEnumerable<DayHours> WeekdayHours { get; set; } 
public class DayHours
{
    public DayHours();

    public string DayName { get; set; }
    public DateTime DayDate { get; set; }
    public DateTime OpenTime { get; set; }
    public DateTime CloseTime { get; set; }
    public bool IsClosed { get; set; }
}

1 个答案:

答案 0 :(得分:0)

您可以IEnumerable代替DictionaryDateTime对象映射到DayHours对象。

通过这种方式,您可以使用DateTime.AddDays()查看第二天​​并从Dictionary中检索出来

@foreach (var weekday in Model.OfficeHours.WeekdayHours)
{
    @weekday.Key <br> // Formerly DayDate, now the key of the Dictionary
    @weekday.Value.OpenTime.ToString("hh:mmtt")<br>
    @weekday.Value.CloseTime.ToString("hh:mmtt")
}

public Dictionary<DateTime,DayHours> WeekdayHours { get; set; } 
public class DayHours
{
    public DayHours();

    public string DayName { get; set; }
    public DateTime OpenTime { get; set; }
    public DateTime CloseTime { get; set; }
    public bool IsClosed { get; set; }
}

您可以选中isClosed,然后显示WeekDayHours[weekday.Key.AddDays(1)].Value...或仅显示weekday.Value...