CSharp examples for System:DateTime Format
Returns a friendly elapsed time string.
// Copyright by the Spark Development Network using System;// w ww . j a va 2 s . c o m public class Main{ /// <summary> /// Returns a friendly elapsed time string. /// </summary> /// <param name="dateTime">The date time.</param> /// <param name="condensed">if set to <c>true</c> [condensed].</param> /// <param name="includeTime">if set to <c>true</c> [include time].</param> /// <returns></returns> public static string ToElapsedString( this DateTime dateTime, bool condensed = false, bool includeTime = true ) { DateTime start = dateTime; DateTime end = RockDateTime.Now; string direction = " Ago"; TimeSpan timeSpan = end.Subtract( start ); if ( timeSpan.TotalMilliseconds < 0 ) { direction = " From Now"; start = end; end = dateTime; timeSpan = timeSpan.Negate(); } string duration = ""; if ( timeSpan.TotalHours < 24 && includeTime ) { // Less than one second if ( timeSpan.TotalSeconds <= 1 ) duration = string.Format( "1{0}", condensed ? "sec" : " Second" ); else if ( timeSpan.TotalSeconds < 60 ) duration = string.Format( "{0:N0}{1}", Math.Truncate( timeSpan.TotalSeconds ), condensed ? "sec" : " Seconds" ); else if ( timeSpan.TotalMinutes < 2 ) duration = string.Format( "1{0}", condensed ? "min" : " Minute" ); else if ( timeSpan.TotalMinutes < 60 ) duration = string.Format( "{0:N0}{1}", Math.Truncate( timeSpan.TotalMinutes ), condensed ? "min" : " Minutes" ); else if ( timeSpan.TotalHours < 2 ) duration = string.Format( "1{0}", condensed ? "hr" : " Hour" ); else if ( timeSpan.TotalHours < 24 ) duration = string.Format( "{0:N0}{1}", Math.Truncate( timeSpan.TotalHours ), condensed ? "hr" : " Hours" ); } if ( duration == "" ) { if ( timeSpan.TotalDays <= 1 ) duration = string.Format( "1{0}", condensed ? "day" : " Day" ); else if ( timeSpan.TotalDays < 31 ) duration = string.Format( "{0:N0}{1}", Math.Truncate( timeSpan.TotalDays ), condensed ? "days" : " Days" ); else if ( end.TotalMonths( start ) <= 1 ) duration = string.Format( "1{0}", condensed ? "mon" : " Month" ); else if ( end.TotalMonths( start ) <= 18 ) duration = string.Format( "{0:N0}{1}", end.TotalMonths( start ), condensed ? "mon" : " Months" ); else if ( end.TotalYears( start ) <= 1 ) duration = string.Format( "1{0}", condensed ? "yr" : " Year" ); else duration = string.Format( "{0:N0}{1}", end.TotalYears( start ), condensed ? "yrs" : " Years" ); } return duration + ( condensed ? "" : direction ); } /// <summary> /// Returns a friendly elapsed time string. /// </summary> /// <param name="dateTime">The date time.</param> /// <param name="condensed">if set to <c>true</c> [condensed].</param> /// <param name="includeTime">if set to <c>true</c> [include time].</param> /// <returns></returns> public static string ToElapsedString( this DateTime? dateTime, bool condensed = false, bool includeTime = true ) { if ( dateTime.HasValue ) { return ToElapsedString( dateTime.Value, condensed, includeTime ); } else return string.Empty; } /// <summary> /// The total years. /// </summary> /// <param name="start">The start.</param> /// <param name="end">The end.</param> /// <returns></returns> public static int TotalYears( this DateTime end, DateTime start ) { return ( end.Year ) - ( start.Year ); } /// <summary> /// The total months. /// </summary> /// <param name="start">The start.</param> /// <param name="end">The end.</param> /// <returns></returns> public static int TotalMonths( this DateTime end, DateTime start ) { return ( end.Year * 12 + end.Month ) - ( start.Year * 12 + start.Month ); } }