解析c#显示友好时间的实现代码
程序员文章站
2023-12-17 18:30:52
复制代码 代码如下:const int second = 1;const int minute = 60 * second;const int hour = 60 * mi...
复制代码 代码如下:
const int second = 1;
const int minute = 60 * second;
const int hour = 60 * minute;
const int day = 24 * hour;
const int month = 30 * day;
if (delta < 0)
{
return "not yet";
}
if (delta < 1 * minute)
{
return ts.seconds == 1 ? "1秒前" : ts.seconds + "秒前";
}
if (delta < 2 * minute)
{
return "1分钟之前";
}
if (delta < 45 * minute)
{
return ts.minutes + "分钟";
}
if (delta < 90 * minute)
{
return "1小时前";
}
if (delta < 24 * hour)
{
return ts.hours + "小时前";
}
if (delta < 48 * hour)
{
return "昨天";
}
if (delta < 30 * day)
{
return ts.days + " 天之前";
}
if (delta < 12 * month)
{
int months = convert.toint32(math.floor((double)ts.days / 30));
return months <= 1 ? "一个月之前" : months + "月之前";
}
else
{
int years = convert.toint32(math.floor((double)ts.days / 365));
return years <= 1 ? "一年前" : years + "年前";
}