Click here to Skip to main content
15,886,362 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.87/5 (12 votes)
17 May 2014CPOL 24.4K   19   10
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)


Written By
Technical Lead
India India
Project Lead
MCTS - .NET Framework 4.0, Web Applications

Blog : http://thakkermukund.wordpress.com
Twitter@thakkermukund

Don't code today, what you can't debug tomorrow!
Everything makes sense in someone's mind

Comments and Discussions

 
QuestionElse redundant after Return Pin
fredatcodeproject20-May-14 0:28
professionalfredatcodeproject20-May-14 0:28 
GeneralMy vote of 5 Pin
HaBiX18-May-14 23:03
HaBiX18-May-14 23:03 
QuestionSuggestions for improvement... Pin
AlexCode18-May-14 21:47
professionalAlexCode18-May-14 21:47 
AnswerRe: Suggestions for improvement... Pin
AlexCode20-May-14 20:27
professionalAlexCode20-May-14 20:27 
GeneralMy vote of 5 Pin
Sunasara Imdadhusen18-May-14 21:24
professionalSunasara Imdadhusen18-May-14 21:24 
GeneralVery useful Pin
Nirav Prabtani18-May-14 20:06
professionalNirav Prabtani18-May-14 20:06 
GeneralMy Vote of 5 Pin
Farhang Fazeli18-May-14 20:06
Farhang Fazeli18-May-14 20:06 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun18-May-14 18:58
Humayun Kabir Mamun18-May-14 18:58 
SuggestionNice but Pin
Xmen Real 18-May-14 5:56
professional Xmen Real 18-May-14 5:56 
GeneralRe: Nice but Pin
Mukund Thakker18-May-14 18:58
professionalMukund Thakker18-May-14 18:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.