This method returns true if the Target Date comes AFTER the source Date. - CSharp System

CSharp examples for System:DateTime Calculate

Description

This method returns true if the Target Date comes AFTER the source Date.

Demo Code


using System.IO;//from  w ww. j a v  a2 s .  c  o  m
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        #endregion
            
            #region IsAfter(DateTime sourceDate, DateTime targetDate, bool includeTime = true)
            /// <summary>
            /// This method returns true if the Target Date comes AFTER the source Date.
            /// </summary>
            public static bool IsAfter(DateTime sourceDate, DateTime targetDate, bool includeTime = true)
            {
                // initial value
                bool isAfter = 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
                isAfter = (sourceDate.Date > targetDate.Date);

                // return value
                return isAfter;
            }
}

Related Tutorials