Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

How to Convert a Date Time to “X minutes ago” in C#

4.87/5 (12 votes)
17 May 2014CPOL 25.6K  
In this post, we will see how we can convert date time in X Time ago format in C#.

Introduction

In this post, we will see how we can convert date time in "X Time ago" format in C#. We will write a C# function that will take a DateTime as string as a parameter and return the appropriate string.

Using the Code

The function to convert DateTime to a “getTimeAgostring is as below:

C#
 public static string getTimeAgo(DateTime strDate)
    {
        string strTime = string.Empty;
        if (IsDate(Convert.ToString(strDate)))
        {
            TimeSpan t = DateTime.UtcNow - Convert.ToDateTime(strDate);
            double deltaSeconds = t.TotalSeconds;

            double deltaMinutes = deltaSeconds / 60.0f;
            int minutes;

            if (deltaSeconds < 5)
            {
                return "Just now";
            }
            else if (deltaSeconds < 60)
            {
                return Math.Floor(deltaSeconds) + " seconds ago";
            }
            else if (deltaSeconds < 120)
            {
                return "A minute ago";
            }
            else if (deltaMinutes < 60)
            {
                return Math.Floor(deltaMinutes) + " minutes ago";
            }
            else if (deltaMinutes < 120)
            {
                return "An hour ago";
            }
            else if (deltaMinutes < (24 * 60))
            {
                minutes = (int)Math.Floor(deltaMinutes / 60);
                return minutes + " hours ago";
            }
            else if (deltaMinutes < (24 * 60 * 2))
            {
                return "Yesterday";
            }
            else if (deltaMinutes < (24 * 60 * 7))
            {
                minutes = (int)Math.Floor(deltaMinutes / (60 * 24));
                return minutes + " days ago";
            }
            else if (deltaMinutes < (24 * 60 * 14))
            {
                return "Last week";
            }
            else if (deltaMinutes < (24 * 60 * 31))
            {
                minutes = (int)Math.Floor(deltaMinutes / (60 * 24 * 7));
                return minutes + " weeks ago";
            }
            else if (deltaMinutes < (24 * 60 * 61))
            {
                return "Last month";
            }
            else if (deltaMinutes < (24 * 60 * 365.25))
            {
                minutes = (int)Math.Floor(deltaMinutes / (60 * 24 * 30));
                return minutes + " months ago";
            }
            else if (deltaMinutes < (24 * 60 * 731))
            {
                return "Last year";
            }

            minutes = (int)Math.Floor(deltaMinutes / (60 * 24 * 365));
            return minutes + " years ago";
        }
        else
        {
            return "";
        }
    } 
public static bool IsDate(string o)
    {
        DateTime tmp;
        return DateTime.TryParse(o, out tmp);
    } 

You can call the function something like below:

C#
getTimeAgo(DateTime.Now.ToString()); 

The output looks something like below:

C#
Just now

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)