Calculate Time Difference as XXX ago - CSharp System

CSharp examples for System:DateTime Calculate

Description

Calculate Time Difference as XXX ago

Demo Code


using System.Web;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;//from  ww  w .  j  a v a  2 s . co m

public class Main{
        public static string CalculateTimeDiff(this DateTime date)
        {
            string ret_string = string.Empty;
            TimeSpan diff = DateTime.UtcNow.Subtract(date);
            double date1 = diff.TotalDays;
            double date2 = diff.TotalHours;
            double date3 = diff.TotalMinutes;
            double date4 = ((date.Year - DateTime.UtcNow.Year) * 12) + date.Month - DateTime.UtcNow.Month;
            double date5 = date.Year - DateTime.UtcNow.Year;

            int _date1 = (int)date1;
            int _date2 = (int)date2;
            int _date3 = (int)date3;
            int _date4 = (int)date4;
            int _date5 = (int)date5;

            if (date5 >= 1)
            {
                if (_date5 > 1)
                {
                    ret_string = _date5.ToString() + " Years ago";
                }
                else {
                    ret_string = _date5.ToString() + " Year ago";
                }
            }
            else if (date4 >= 1)
            {
                if (_date4 > 1)
                {
                    ret_string = _date4.ToString() + " Months ago";
                }
                else
                {
                    ret_string = _date4.ToString() + " Month ago";
                }
            }
            else if (date1 >= 1)
            {
                ret_string = _date1.ToString() + " Days ago";

                if (date1 >= 7)
                {
                    int weeks = _date1 / 7;
                    if (weeks > 1)
                    {
                        ret_string = weeks.ToString() + " Weeks ago";
                    }
                    else {
                        ret_string = weeks.ToString() + " Week ago";
                    }
                }
            }
            else
            {
                if (date2 >= 1)
                {
                    if (_date2 > 1)
                    {
                        ret_string = _date2.ToString() + " Hours ago";
                    }
                    else {
                        ret_string = _date2.ToString() + " Hour ago";
                    }
                }
                else if (date3 >= 1)
                {
                    if (_date3 > 1)
                    {
                        ret_string = _date3.ToString() + " Minute ago";
                    }
                    else {
                        ret_string = _date3.ToString() + " Minut ago";
                    }
                }
                else {
                    ret_string = " Just now";
                }
            }

            return ret_string;
        }
}

Related Tutorials