Click here to Skip to main content
Click here to Skip to main content

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

, 17 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

 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:

getTimeAgo(DateTime.Now.ToString()); 

The output looks something like below:

 Just now   

License

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

Share

About the Author

Mukund Thakker
Technical Lead
India India
Tech 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
Follow on   Twitter

Comments and Discussions

 
QuestionElse redundant after Return Pinmemberfredatcodeproject20-May-14 1:28 
GeneralMy vote of 5 PinmemberHaBiX19-May-14 0:03 
QuestionSuggestions for improvement... PinprofessionalAlexCode18-May-14 22:47 
AnswerRe: Suggestions for improvement... PinprofessionalAlexCode20-May-14 21:27 
GeneralMy vote of 5 PinprofessionalSunasara Imdadhusen18-May-14 22:24 
GeneralVery useful PinprofessionalNirav Prabtani18-May-14 21:06 
GeneralMy Vote of 5 PinmemberFarhang Fazeli18-May-14 21:06 
GeneralMy vote of 5 PinmemberHumayun Kabir Mamun18-May-14 19:58 
SuggestionNice but PinmemberXmen W.K.18-May-14 6:56 
GeneralRe: Nice but PinprofessionalMukund Thakker18-May-14 19:58 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 17 May 2014
Article Copyright 2014 by Mukund Thakker
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid