How can I convert Date to String formate.?

Possible duplicate:
How to calculate the relative time?

I want to convert the Date value to a string format, just like YouTube Video Uploaded Time or Date, for example. 2 Years Ago or 1 month ago or 8 houre ago, so just assume that I have a simple date as a way out.

Thank..!!

+4
source share
7 answers

I wrote this function several years ago, it looks like you did 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 another question. ( Calculate relative time in C # )

+1
source
DateTime now = DateTime.Now;

DateTime older = //orignal date time;

TimeSpan difference = now.Subtract(older);

, , , .., ,

+2

, - timeago algorithim.

- :

    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";
    }

Stackoverflow , :

#

.:

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

, .

+1

. TimeSpan, . , TimeSpan :

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 up to you.
+1
0

, , : , DateTime, DateTime.Now , .

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 select DateTime.Now.HowMuchAgo ​​(), you will get "Just Now!". Of course, you can change this to add additional if statements to get additional detail in the descriptions or to change what is specified in the descriptions.

0
source

All Articles