Returns a double indicating the number of days between two dates (past is negative) - CSharp System

CSharp examples for System:DateTime Day

Description

Returns a double indicating the number of days between two dates (past is negative)

Demo Code

//   The contents of this file are subject to the New BSD
using System;//from w  w w . j a  va 2s.c om

public class Main{
        /// <summary>
    /// Returns a double indicating the number of days between two dates (past is negative)
    /// </summary>
    /// <param name="dateOne">The date one.</param>
    /// <param name="dateTwo">The date two.</param>
    /// <returns></returns>
    public static double DiffMinutes(this DateTime dateOne, DateTime dateTwo) {
        return Diff(dateOne, dateTwo).TotalMinutes;
    }
        /// <summary>
    /// Returns a double indicating the number of days between two dates (past is negative)
    /// </summary>
    /// <param name="dateOne">The date one.</param>
    /// <param name="dateTwo">The date two.</param>
    /// <returns></returns>
    public static double DiffMinutes(this string dateOne, string dateTwo) {
        DateTime dtOne;
        DateTime dtTwo;
        if (DateTime.TryParse(dateOne, out dtOne) && DateTime.TryParse(dateTwo, out dtTwo))
            return Diff(dtOne, dtTwo).TotalMinutes;
        return 0;
    }
    /// <summary>
    /// Diffs the specified date.
    /// </summary>
    /// <param name="dateOne">The date one.</param>
    /// <param name="dateTwo">The date two.</param>
    /// <returns></returns>
    public static TimeSpan Diff(this DateTime dateOne, DateTime dateTwo) {
        TimeSpan t = dateOne.Subtract(dateTwo);
        return t;
    }
}

Related Tutorials