如何将Date转换为String formate。
可能重复:
我如何计算相对时间?
我想将日期值转换为字符串格式,就像YouTubevideo上传时间或日期格式一样。 2年前或1个月前或8个月前这样的假设我只有简单的日期作为输出。
谢谢..!!
我几年前写过这个函数,看起来就是你所追求的。
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"; }
有关更完整/详细的function,请参阅其他问题。 ( 计算C#中的相对时间 )
DateTime now = DateTime.Now; DateTime older = //orignal date time; TimeSpan difference = now.Subtract(older);
获得时间跨度后,您可以使用属性时间跨度类公开计算年,月,日等
我想你正在寻找像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上有类似的问题 ,你必须看到:
计算C#中的相对时间
有关更多详情,请参阅
http://www.daniweb.com/software-development/csharp/threads/127213
希望这可以帮助。
您需要从当前日期中减去日期。 这将为您提供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.
从这里开始; 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来确定现在和发布日期之间的差异。 您将不得不对结果进行一些工作,但您可以计算出不同的小时/分钟/天/年。
检查一下:自定义日期和时间格式字符串
我觉得我正在解决你的作业,但是这里:以下是一个扩展方法的示例,该方法采用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(),你会得到“刚才!”。 当然,您可以修改此项以添加更多if语句,以在说明中获得更多粒度或修改说明中所述内容。