c#显示友好时间代码

复制代码
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 + "年前";
}
复制代码

转载: http://outofmemory.cn/code-snippet/973/c-display-youhao-time-code

posted @   玉开  阅读(3364)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2008-11-24 (转)如何提高google adsense单价
2008-11-24 你真的理解T-sql中的NULL吗?
点击右上角即可分享
微信分享提示