C# 格式化的日期时间,例如“日期时间”;自去年以来”;
我正在尝试打印帖子的日期时间,但以这样的格式(从一个小时开始,从昨天开始,或者从去年开始),我使用以下代码成功地完成了打印。C# 格式化的日期时间,例如“日期时间”;自去年以来”;,c#,asp.net,asp.net-mvc,asp.net-core,asp.net-web-api,C#,Asp.net,Asp.net Mvc,Asp.net Core,Asp.net Web Api,我正在尝试打印帖子的日期时间,但以这样的格式(从一个小时开始,从昨天开始,或者从去年开始),我使用以下代码成功地完成了打印。 但是,有没有更好的方法可以像下面的代码那样执行此操作?? 注意:此代码中有一个名为Date的属性 public string GetFormattedDate() { var now = DateTime.Now; var years = now.Year - Date.Year; // Calculating leap year if
但是,有没有更好的方法可以像下面的代码那样执行此操作??
注意:此代码中有一个名为Date的属性
public string GetFormattedDate()
{
var now = DateTime.Now;
var years = now.Year - Date.Year;
// Calculating leap year
if (Date.Date > now.AddYears(-years)) years--;
if (years > 0)
{
return "Since " + ((years > 1) ? $"{years} years" : "last year");
}
var months = now.Month - Date.Month;
if (months > 0)
{
return "Since " + ((months > 1) ? $"{months} months" : "last month");
}
var days = now.Day - Date.Day;
if (days > 0)
{
return "Since " + ((days > 1) ? $"{days} days" : "yesterday");
}
var hours = now.Hour - Date.Hour;
if (hours > 0)
{
return "Since " + ((hours > 1) ? $"{hours} hours" : "one hour");
}
var minutes = Math.Abs(now.Minute - Date.Minute);
if (minutes > 0)
{
return "Since " + ((minutes > 1) ? $"{minutes} minutes" : "one minute");
}
return "Now";
}
这是另一个比我好得多的算法。
感谢所有的贡献者,当然还有
公共静态类DateTimePersonalize
{
公共静态字符串人性化(此DateTime输入,DateTime比较数据库)
{
var ts=新的时间跨度(Math.Abs(comparisonBase.Ticks-input.Ticks));
整数秒=秒,分钟=分钟,小时=小时,天=天;
整数年=0,月=0;
//从较小的单元向较大的单元过渡
如果(ts.毫秒>=999)
{
秒+=1;
}
如果(秒>=59)
{
分钟+=1;
}
如果(分钟>=59)
{
小时数+=1;
}
如果(小时数>=23)
{
天数+=1天;
}
//月份计算
如果(天数>=30天&31天&365天)
{
var系数=换算成32(数学下限((双)天/30));
var maxMonths=转换为32(数学上限((双)天/30));
月=(天>=30*因子)?最大月数:最大月数-1;
}
//年份计算
如果(天数>=365天&&365天)
{
var系数=换算成32(数学下限((双)天/365));
var maxMonths=Convert.ToInt32(数学上限((双)天/365));
年=(天>=365*因子)?最大月数:最大月数-1;
}
如果(年数>0)
{
返回“自”+((年数>1)?$“{years}年”:“去年”);
}
如果(月数>0)
{
返回“自”+((月数>1)?$“{months}个月”:“上个月”);
}
如果(天数>0)
{
返回“自”+((天>1)$“{days}天”:“昨天”);
}
如果(小时数>0)
{
返回“自”+((小时数>1)$”{hours}hours:“一小时”);
}
如果(分钟>0)
{
返回“自”+((分钟数>1)?$”{minutes}minutes:“一分钟”);
}
返回“现在”;
}
公共静态字符串相对日期(日期时间)
{
字典阈值=新字典();
整数分钟=60;
整小时=60*分钟;
整数天=24*小时;
添加(60,{0}秒前);
添加(分钟*2,“一分钟前”);
添加(45*分钟,{0}分钟前);
添加(120*分钟,“一小时前”);
添加(天,{0}小时前”);
添加(第2天,“昨天”);
添加(第30天,“{0}天前”);
添加(第365天,“{0}个月前”);
Add(long.MaxValue,“{0}年前”);
long-like=(DateTime.Now.Ticks-theDate.Ticks)/10000000;
foreach(thresholds.Keys中的长阈值)
{
如果(自<阈值)
{
TimeSpan t=新的TimeSpan((DateTime.Now.Ticks-theDate.Ticks));
返回string.Format(阈值[阈值],(t.Days>365?t.Days/365:(t.Days>0?t.Days:(t.Hours>0?t.Hours:(t.Minutes>0?t.Minutes:(t.Seconds>0?t.Seconds:0щщ)).ToString();
}
}
返回“”;
}
根据其他功能是否值得项目的规模,内置了此功能。这很好,但正如您所说,它太大了,不值得。因此,我使用了他们的算法。非常有用,谢谢。
public static class DateTimeHumanize
{
public static string Humanize(this DateTime input, DateTime comparisonBase)
{
var ts = new TimeSpan(Math.Abs(comparisonBase.Ticks - input.Ticks));
int seconds = ts.Seconds, minutes = ts.Minutes, hours = ts.Hours, days = ts.Days;
int years = 0, months = 0;
// start approximate from smaller units towards bigger ones
if (ts.Milliseconds >= 999)
{
seconds += 1;
}
if (seconds >= 59)
{
minutes += 1;
}
if (minutes >= 59)
{
hours += 1;
}
if (hours >= 23)
{
days += 1;
}
// month calculation
if (days >= 30 & days <= 31)
{
months = 1;
}
if (days > 31 && days < 365)
{
var factor = Convert.ToInt32(Math.Floor((double)days / 30));
var maxMonths = Convert.ToInt32(Math.Ceiling((double)days / 30));
months = (days >= 30 * factor) ? maxMonths : maxMonths - 1;
}
// year calculation
if (days >= 365 && days <= 366)
{
years = 1;
}
if (days > 365)
{
var factor = Convert.ToInt32(Math.Floor((double)days / 365));
var maxMonths = Convert.ToInt32(Math.Ceiling((double)days / 365));
years = (days >= 365 * factor) ? maxMonths : maxMonths - 1;
}
if (years > 0)
{
return "Since " + ((years > 1) ? $"{years} years" : "last year");
}
if (months > 0)
{
return "Since " + ((months > 1) ? $"{months} months" : "last month");
}
if (days > 0)
{
return "Since " + ((days > 1) ? $"{days} days" : "yesterday");
}
if (hours > 0)
{
return "Since " + ((hours > 1) ? $"{hours} hours" : "one hour");
}
if (minutes > 0)
{
return "Since " + ((minutes > 1) ? $"{minutes} minutes" : "one minute");
}
return "Now";
}
public static string RelativeDate(DateTime theDate)
{
Dictionary<long, string> thresholds = new Dictionary<long, string>();
int minute = 60;
int hour = 60 * minute;
int day = 24 * hour;
thresholds.Add(60, "{0} seconds ago");
thresholds.Add(minute * 2, "a minute ago");
thresholds.Add(45 * minute, "{0} minutes ago");
thresholds.Add(120 * minute, "an hour ago");
thresholds.Add(day, "{0} hours ago");
thresholds.Add(day * 2, "yesterday");
thresholds.Add(day * 30, "{0} days ago");
thresholds.Add(day * 365, "{0} months ago");
thresholds.Add(long.MaxValue, "{0} years ago");
long since = (DateTime.Now.Ticks - theDate.Ticks) / 10000000;
foreach (long threshold in thresholds.Keys)
{
if (since < threshold)
{
TimeSpan t = new TimeSpan((DateTime.Now.Ticks - theDate.Ticks));
return string.Format(thresholds[threshold], (t.Days > 365 ? t.Days / 365 : (t.Days > 0 ? t.Days : (t.Hours > 0 ? t.Hours : (t.Minutes > 0 ? t.Minutes : (t.Seconds > 0 ? t.Seconds : 0))))).ToString());
}
}
return "";
}