Click here to Skip to main content
13,044,584 members (60,072 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 8 May 2014

How To Convert a Future Date Time to “X minutes from now” in C#

, 8 May 2014
Rate this:
Please Sign up or sign in to vote.
How to convert a future date time to "X minutes from now" in C#

In my previous post, we saw how we can convert a Date Time value to “X Minutes Ago” feature using C#. A user interestingly asked me for a function that can do something similar but for future dates. Today, in this post, we will see how we can achieve similar functionality for a future date in C#. In this post, we will write a C# function that will take a DateTime as parameter and return relevant time remaining in future. This can be a good idea for displaying a list of events/sessions on a webpage.

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

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

You can call the function something like below:


The output looks something like below:


Hope this post helps you. Keep learning and sharing!


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


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...

Comments and Discussions

QuestionHow To Convert a Past Date Time to “X minutesago” in C# Pin
Cuculala1-Jul-14 23:49
memberCuculala1-Jul-14 23:49 
SuggestionFor a more complex (and complete) feature, you can have a look at "Humanizer" Pin
Jérôme VIBERT8-May-14 21:52
memberJérôme VIBERT8-May-14 21:52 
GeneralRe: For a more complex (and complete) feature, you can have a look at "Humanizer" Pin
Nitesh Luharuka9-May-14 4:25
memberNitesh Luharuka9-May-14 4:25 
GeneralRe: For a more complex (and complete) feature, you can have a look at "Humanizer" Pin
shatl10-May-14 2:42
membershatl10-May-14 2:42 
GeneralRe: For a more complex (and complete) feature, you can have a look at "Humanizer" Pin
Jérôme VIBERT10-May-14 8:22
memberJérôme VIBERT10-May-14 8:22 

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.

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