To Relative Time Simple - CSharp System

CSharp examples for System:DateTime Calculate

Description

To Relative Time Simple

Demo Code



public class Main{
        private static string ToRelativeTimeSimple(TimeSpan ts, string sign)
        {//w  w  w  .java 2 s .  c  o  m
            var delta = ts.TotalSeconds;
            if (delta < 1) return "< 1 sec";
            if (delta < 60) return sign + ts.Seconds + " sec" + (ts.Seconds == 1 ? "" : "s");
            if (delta < 3600 /*60 mins * 60 sec*/) return sign + ts.Minutes + " min" + (ts.Minutes == 1 ? "" : "s");
            if (delta < 86400 /*24 hrs * 60 mins * 60 sec*/)
                return sign + ts.Hours + " hour" + (ts.Hours == 1 ? "" : "s");
            return sign + ts.Days + " days";
        }
}

Related Tutorials