我如何将Date转换为String格式。

时间:2012-01-02 13:10:17

标签: c#

  

可能重复:
  How do I calculate relative time?

我想将日期值转换为字符串格式,就像YouTube视频上传时间或日期格式一样。 2年前或1个月前或8个月前这样的假设我只有简单的日期作为输出。

谢谢.. !!

7 个答案:

答案 0 :(得分:2)

DateTime now = DateTime.Now;

DateTime older = //orignal date time;

TimeSpan difference = now.Subtract(older);

获得时间跨度后,您可以使用属性时间跨度类公开计算年,月,日等

答案 1 :(得分:1)

我认为您正在寻找类似 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上有类似的问题,你必须看到:

<强> Calculate relative time in C#

有关详细信息,请参阅:

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

希望这有帮助。

答案 2 :(得分:1)

几年前我写过这个函数,看起来就是你所追求的。

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

有关更完整/详细的功能,请参阅另一个问题。 (Calculate relative time in C#

答案 3 :(得分: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's up to you.

答案 4 :(得分:0)

从这里开始; http://msdn.microsoft.com/en-us/library/system.datetime.aspx

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

我相信你要做的是使用TimeSpan来确定现在和发布日期之间的差异。您将不得不对结果进行一些工作,但您可以计算出不同的小时/分钟/天/年。

答案 5 :(得分:0)

答案 6 :(得分: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();
        }
    }
}

如果你打电话给DateTime.Now.HowMuchAgo​​(),你现在就会得到#34;&#34;。当然,您可以对其进行修改以添加更多if语句,以便在描述中获得更多粒度或修改说明中所述内容。