5

Possible Duplicate:
How do I calculate relative time?

I want to convert Date Value to string formate just like YouTube Videos Uploaded Time or Date for Eg. 2 Years Ago or 1 month ago or 8 houre ago like this just assume I have simple Date as an Output.

Thank you..!!

Community
  • 1
  • 1
Jitendra Jadav
  • 355
  • 1
  • 8
  • 19

6 Answers6

2
DateTime now = DateTime.Now;

DateTime older = //orignal date time;

TimeSpan difference = now.Subtract(older);

Once you get the time span you can calculate years, month, days etc using properties time span class exposes

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
  • Also, you can use the minus (-) operator. You don't have to use Subtract (which is not written correctly above). – Lzh Jan 02 '12 at 13:35
1

I think you're looking for something like timeago algorithim.

You can use something like this:

    static void Main(string[] args)
    {
        Console.WriteLine(GetDifferenceDate(new DateTime(2011, 11, 25, 10, 30, 2), 
            new DateTime(2012, 1, 2, 6, 3, 5)));
    }

    static string GetDifferenceDate(DateTime date1, DateTime date2)
    {
        if (DateTime.Compare(date1, date2) >= 0)
        {
            TimeSpan ts = date1.Subtract(date2);
            return string.Format("{0} days, {1} hours, {2} minutes, {3} seconds", 
                ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
        }
        else
            return "Not valid";
    }

There is a similar questions on Stackoverflow, you must see:

Calculate relative time in C#

For more details see:

http://www.daniweb.com/software-development/csharp/threads/127213

Hope this helps.

Community
  • 1
  • 1
talha2k
  • 24,937
  • 4
  • 62
  • 81
1

I have written this function some years ago, looks like that's what you are after.

public static string GetTimeElpased(int secondsElpased, int minutesElpased, int hoursElpased,
    int daysElpased, int monthsElpased, int yearsElpased)
{
    if (secondsElpased < 30)
        return "few seconds ago";

    if (minutesElpased < 1)
        return secondsElpased + " seconds ago";

    if (minutesElpased < 5)
        return "few minutes ago";

    if (hoursElpased < 1)
        return minutesElpased + " minutes ago";

    if (hoursElpased < 5)
        return "few hours ago";

    if (daysElpased < 1)
        return hoursElpased + " hours ago";

    if (daysElpased == 1)
        return "yesterday";

    if (monthsElpased < 1)
    return daysElpased + " days ago";

    if (monthsElpased == 1)
        return "month ago";

    if (yearsElpased < 1)
        return monthsElpased + " months ago";

    string halfYear = (monthsElpased >= 6) ? " and half" : "";
    if (yearsElpased == 1)
        return "year" + halfYear + " ago";

    return yearsElpased + " years ago";
}

For more complete/detailed functions see the other question. ( Calculate relative time in C# )

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
1

You need to subtract the date from the current date. This will give you a TimeSpan value which represents a difference between two dates. You have to write the logic that turns this TimeSpan into readable text on your own:

TimeSpan d = DateTime.Now - someDate;
if (d.TotalSeconds < 59)
{
  return d.TotalSeconds + " second(s) ago";
}
else if (d.TotalMinutes < 59)
{
  return d.TotalMinutes + " minute(s) ago";
} 
else if (d.TotalHours < 23)
{
  return d.TotalHours + " hour(s) ago";
}

// days, weeks, months and years. It's up to you.
Stefan
  • 14,530
  • 4
  • 55
  • 62
0

Start here; http://msdn.microsoft.com/en-us/library/system.datetime.aspx

http://social.msdn.microsoft.com/search/en-us?query=TimeSpan&x=0&y=0

I believe what you're trying to do is use TimeSpan to determine the difference between now and the date something was posted. You're going to have to do some work with the results, but you can calculate how many hours/minutes/days/years there are different.

tbddeveloper
  • 2,407
  • 1
  • 23
  • 39
0

I feel like I'm solving your homework but here: the following is an example for an extension method that takes a DateTime object, compares it with the DateTime.Now and returns appropriate description of how long ago was the date.

public static class Ext
{
    public static string HowMuchAgo(this DateTime dt)
    {
        var difference = (DateTime.Now - dt);
        if (difference < TimeSpan.FromSeconds(.5))
        {
            return "Just now!";
        }
        else if (difference < TimeSpan.FromMinutes(1))
        {
            return difference.Seconds + " seconds ago.";
        }
        else if (difference < TimeSpan.FromHours(1))
        {
            return difference.Minutes + " minutes and " + difference.Seconds + " seconds ago.";
        }
        else if (difference < TimeSpan.FromDays(1))
        {
            return difference.Hours + " hours and " + difference.Minutes + " minutes and " + difference.Seconds + " seconds ago.";
        }
        else if (difference > TimeSpan.FromDays(1) && difference < TimeSpan.FromDays(2))
        {
            return "Yesterday at " + dt.ToShortTimeString();
        }
        else
        {
            return "On " + dt.ToString();
        }
    }
}

If you call DateTime.Now.HowMuchAgo() you'll get "Just now!". Of course you can modify this to add more if statements to get more granularity in the descriptions or modify what is stated in the descriptions.

Lzh
  • 3,585
  • 1
  • 22
  • 36