To Days Ago - CSharp System

CSharp examples for System:DateTime Day

Description

To Days Ago

Demo Code



public class Main{
        public static string ToDaysAgo(this DateTime d)
        {//from w  ww  .  j  a v a  2  s  .  c o  m
            var dt = DateTime.Now.Subtract(d.ToLocalTime());
            if (dt.TotalDays > 1)
                return Convert.ToInt32(dt.TotalDays) + " days ago";
            if (dt.TotalHours > 1)
                return Convert.ToInt32(dt.TotalHours) + " hours ago";
            if (dt.TotalMinutes > 1)
                return Convert.ToInt32(dt.TotalMinutes) + " minutes ago";
            return "moments ago";
        }
}

Related Tutorials