惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
G
Google Developers Blog
D
Docker
The GitHub Blog
The GitHub Blog
H
Help Net Security
WordPress大学
WordPress大学
博客园_首页
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
罗磊的独立博客
I
InfoQ
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
Jina AI
Jina AI
J
Java Code Geeks
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News

博客园 - 玉开

好简历 5 月下旬更新:React 简历编辑器、95 套模板、全新编辑体验 好简历是干啥的?一篇说人话的自我介绍 代码评审歪诗 京东招聘架构师 暂停招聘 京东招聘Java开发人员 如何准确判断请求是搜索引擎爬虫(蜘蛛)发出的请求? [已招满,谢谢关注]Sogou招聘C#开发人员,有意者请进 maven仓库信息分析站点推荐 技术人员如何确认web产品的需求 有你救有力量,呼吁添加壹基金在支付宝公益开通的救援四川雅安banner 360以安全之名做搜索,可信,还是欺世盗名? OutOfMemory.CN 为程序员服务 上线40天小记 js+JQuery实现返回顶部功能 c#使用System.Media.SoundPlayer播放资源文件中的wav文件 C#使用Process类杀死进程,执行命令等 推荐一个新的技术网站OutOfMemory.CN 在32位windows 7下安装PyLucene linux无root权限安装软件 error rpm cannot be installed 端口被占用了,使用netstat找到占用端口的进程
c#显示友好时间代码
玉开 · 2012-11-24 · via 博客园 - 玉开
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 @ 2012-11-24 20:04  玉开  阅读(3405)  评论()    收藏  举报