Click here to Skip to main content
12,697,178 members (25,445 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

23.2K views
18 bookmarked
Posted

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

, 7 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to Convert a Date Time to “X minutes ago” in C#

In one of our previous posts, we saw how can we convert a Date Time value to “X Minutes Ago” feature using jQuery. Today, in this post, we will see how we can achieve the same functionality in C#. In this post, we will write a C# function that will take a DateTime as a parameter and return the appropriate string.

The function to convert DateTime to a “Time Ago” string is as below:

public static string TimeAgo(DateTime dt)
{
    TimeSpan span = DateTime.Now - dt;
    if (span.Days > 365)
    {
        int years = (span.Days / 365);
        if (span.Days % 365 != 0)
            years += 1;
        return String.Format("about {0} {1} ago", 
        years, years == 1 ? "year" : "years");
    }
    if (span.Days > 30)
    {
        int months = (span.Days / 30);
        if (span.Days % 31 != 0)
            months += 1;
        return String.Format("about {0} {1} ago", 
        months, months == 1 ? "month" : "months");
    }
    if (span.Days > 0)
        return String.Format("about {0} {1} ago", 
        span.Days, span.Days == 1 ? "day" : "days");
    if (span.Hours > 0)
        return String.Format("about {0} {1} ago", 
        span.Hours, span.Hours == 1 ? "hour" : "hours");
    if (span.Minutes > 0)
        return String.Format("about {0} {1} ago", 
        span.Minutes, span.Minutes == 1 ? "minute" : "minutes");
    if (span.Seconds > 5)
        return String.Format("about {0} seconds ago", span.Seconds);
    if (span.Seconds <= 5)
        return "just now";
    return string.Empty;
}

You can call the function something like below:

Console.WriteLine(TimeAgo(DateTime.Now));
Console.WriteLine(TimeAgo(DateTime.Now.AddMinutes(-5)));
Console.WriteLine(TimeAgo(DateTime.Now.AddMinutes(-59)));
Console.WriteLine(TimeAgo(DateTime.Now.AddHours(-2)));
Console.WriteLine(TimeAgo(DateTime.Now.AddDays(-5)));
Console.WriteLine(TimeAgo(DateTime.Now.AddMonths(-3)));

The output looks something like below:

datetime-time-ago-C#

Hope this post helps you. Keep learning and sharing!

License

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

Share

About the Author

Nitesh Luharuka
Team Leader Director @ Rebin Infotech
India India
An Independent IT Professional and Consultant on MS Technologies like .Net | MVC | Silverlight | Sharepoint | MS Project Server along with open source CMS Systems like DotNetNuke etc.

I am an enthusiastic technocrat who love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionComment Pin
Mohan Rajesh.K5-Nov-14 3:11
memberMohan Rajesh.K5-Nov-14 3:11 
AnswerRe: Comment Pin
Nitesh Luharuka5-Nov-14 3:33
memberNitesh Luharuka5-Nov-14 3:33 
QuestionHow about "X minutes from now"? Pin
Member 96667288-May-14 7:51
memberMember 96667288-May-14 7:51 
AnswerRe: How about "X minutes from now"? Pin
Nitesh Luharuka8-May-14 7:56
memberNitesh Luharuka8-May-14 7:56 
AnswerRe: How about "X minutes from now"? Pin
Nitesh Luharuka8-May-14 19:43
memberNitesh Luharuka8-May-14 19:43 
GeneralRe: How about "X minutes from now"? Pin
Member 107476419-May-14 2:10
memberMember 107476419-May-14 2:10 
QuestionManagement of <5s Pin
romlel598-May-14 4:32
memberromlel598-May-14 4:32 
AnswerRe: Management of <5s Pin
Nitesh Luharuka8-May-14 4:56
memberNitesh Luharuka8-May-14 4:56 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 7 May 2014
Article Copyright 2014 by Nitesh Luharuka
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid