c#显示友好时间代码

来自:http://outofmemory.cn/code-snippet/973/c-display-youhao-time-code

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 + "年前";
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值