Returns true if the DateTime is within X number of days, false otherwise. - CSharp System

CSharp examples for System:DateTime Day

Description

Returns true if the DateTime is within X number of days, false otherwise.

Demo Code


using System;//from   w  w w  . j a  v  a2 s. c o  m

public class Main{
        /// <summary>
        /// Returns true if the DateTime is within X number of days, false otherwise.
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="days"></param>
        /// <param name="useUtc"></param>
        /// <returns></returns>
        public static bool IsWithinXDays(this DateTime dt, int days, bool useUtc = true)
        {
            DateTime past = DateTime.Today.AddDays(-days).ToUniversalTime();
            if (!useUtc) past = DateTime.Today.AddDays(-days);

            return dt.CompareTo(past) >= 0;
        }
}

Related Tutorials