This method returns true if the source Date comes BEFORE the target Date. - CSharp System

CSharp examples for System:DateTime Calculate

Description

This method returns true if the source Date comes BEFORE the target Date.

Demo Code


using System.IO;/*from w  w  w .j  a  v  a 2 s.c om*/
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        #endregion

            #region IsBefore(DateTime sourceDate, DateTime targetDate, bool includeTime = true)
            /// <summary>
            /// This method returns true if the sourceDate comes BEFORE the targetDate.
            /// </summary>
            public static bool IsBefore(DateTime sourceDate, DateTime targetDate, bool includeTime = true)
            {
                // initial value
                bool isBefore = false;

                // if time should be Not be included in the comparison
                if (!includeTime)
                {
                    // recreate the sourceDate without the time
                    sourceDate = new DateTime(sourceDate.Year, sourceDate.Month, sourceDate.Day);

                    // recreate the targetDate without the time
                    targetDate = new DateTime(targetDate.Year, targetDate.Month, targetDate.Day);
                }

                // compare just the dates
                isBefore = (sourceDate.Date < targetDate.Date);

                // return value
                return isBefore;
            }
}

Related Tutorials