Get DateTime Range - CSharp System

CSharp examples for System:TimeSpan

Description

Get DateTime Range

Demo Code


using System.Linq;
using System;//from www  .  j  a va  2 s.  c om

public class Main{
        public static DateTimeRange GetRange(DateTime? from, DateTime? to, int dayRange)
        {
            DateTime fromDate = DateTime.UtcNow.Date.AddDays(-dayRange);
            DateTime toDate = DateTime.UtcNow.Date.AddDays(+dayRange);
            if (from.HasValue)
            {
                fromDate = from.Value.Date;
            }
            if (to.HasValue)
            {
                toDate = to.Value.Date;
            }
            return new DateTimeRange(fromDate, toDate);
        }
        public static DateTimeRange GetRange(DateTime? from, DateTime? to)
        {
            return GetRange(@from, to, 1);
        }
}

Related Tutorials