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

推荐订阅源

Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
J
Java Code Geeks
L
LangChain Blog
V
V2EX
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
博客园 - Franky
Microsoft Azure Blog
Microsoft Azure Blog
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
博客园 - 司徒正美
B
Blog
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog

博客园 - 玉开

好简历 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)  评论()    收藏  举报