Is Time This Day - CSharp System

CSharp examples for System:DateTime Day

Description

Is Time This Day

Demo Code


using System.Globalization;
using System;/*from  ww  w.j  av  a2 s  .c o  m*/

public class Main{
        public static bool IsTimeThisDay(
            this DateTime? time, 
            DateTime? day, 
            Tuple<DateTime?, DateTime?> range = null)
        {
            if (!time.HasValue || !day.HasValue) return true; // if time is not set we asume it goes to all days

            var t = time.Value;
            var nextDay = day.Value.AddDays(1);
            var firstDay = range != null ? range.Item1 : null;
            var lastDay = range != null ? range.Item2 ?? range.Item1 : null; // using first day as last one to do not lose some out-of-range shows

            var result =
                (firstDay != null && t.Date <= firstDay && day == firstDay) || // times ahead of first day
                (t.Date == day && t.Hour >= 6) ||
                (t.Date == nextDay && t.Hour < 6) || // late night times go to next day
                (lastDay != null && t.Date >= lastDay && day == lastDay); // times behind of last day

            return result;
        }
        public static bool IsTimeThisDay(
            this DateTime time,
            DateTime? day,
            Tuple<DateTime?, DateTime?> range = null)
        {
            return IsTimeThisDay((DateTime?)time, day, range);
        }
}

Related Tutorials