This method will return an integer representing the number of days between two particular dates passed to a method - CSharp System

CSharp examples for System:DateTime Day

Description

This method will return an integer representing the number of days between two particular dates passed to a method

Demo Code


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

public class Main{
        /// <summary>
        /// This method will return an integer representing the number of days between two
        /// particular dates passed to a method
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static int GetDaysDifference(DateTime startDate, DateTime endDate)
        {
            TimeSpan timeDifference = endDate - startDate;

            try
            {
                return Convert.ToInt32(Math.Round(timeDifference.TotalDays));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
}

Related Tutorials