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

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

博客园 - MangaGo

在网页里让文本框只能输入数字的一种方法。外加回车换Tab - MangaGo - 博客园 (转) WINDOWS下的使用VS.NET2005的SVN手记 学习.Net的经典网站 Subversion快速入门教程 ASP.NET 2.0 下配置 FCKeditor - MangaGo asp中验证码图片为BMP格式,但是不显示 [Serializable]在C#中的作用-NET 中的对象序列化 VSS服务器安装配置 javascript判断密码安全级别 document.getElementById 你真正了解了吗 - MangaGo - 博客园 js获得当前日期并 显示 - MangaGo - 博客园 永远不要跟父母说的十句话,谁没说过? 在ASP.NET里轻松实现缩略图 用ASP.NET缓存提高站点性能【转载】 一个sql中常遇到的表结构转换问题 一个冒泡排序算法 一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现 阶乘的经典算法! 关于上传文件的大小限制
TimeSpan 用法 求离最近发表时间的函数 - MangaGo
MangaGo · 2008-03-03 · via 博客园 - MangaGo


求离最近发表时间的函数

public string DateStringFromNow(DateTime dt)    
{    
TimeSpan span 
= DateTime.Now - dt;    
if (span.TotalDays > 60)    
{    
return dt.ToShortDateString();    
}
    
else if ( span.TotalDays > 30 )    
{    
return "1个月前";    
}
    
else if (span.TotalDays > 14)    
{    
return "2周前";    
}
    
else if (span.TotalDays > 7)    
{    
return "1周前";    
}
    
else if (span.TotalDays > 1)    
{    
return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));    
}
    
else if (span.TotalHours > 1)    
{    
return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));    
}
    
else if (span.TotalMinutes > 1)    
{    
return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));    
}
    
else if (span.TotalSeconds >= 1)    
{    
return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));    
}
    
else   
{    
return "1秒前";    
}
    
}
  

C#中使用TimeSpan计算两个时间的差值

可以反加两个日期之间任何一个时间单位。
private string DateDiff(DateTime DateTime1, DateTime DateTime2)
{string dateDiff = null;
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
dateDiff = ts.Days.ToString()+"天"+ ts.Hours.ToString()+"小时"+ ts.Minutes.ToString()+"分钟"+ ts.Seconds.ToString()+"秒";
return dateDiff;
}

TimeSpan    ts    =    Date1    -    Date2;   
   double    dDays    =    ts.TotalDays;//带小数的天数,比如1天12小时结果就是1.5   
   int    nDays    =    ts.Days;//整数天数,1天12小时或者1天20小时结果都是1   

说明:
1.DateTime值类型代表了一个从公元0001年1月1日0点0分0秒到公元9999年12月31日23点59分59秒之间的具体日期时刻。因此,你可以用DateTime值类型来描述任何在想象范围之内的时间。一个DateTime值代表了一个具体的时刻
2.TimeSpan值包含了许多属性与方法,用于访问或处理一个TimeSpan值
下面的列表涵盖了其中的一部分:
Add:与另一个TimeSpan值相加。
Days:返回用天数计算的TimeSpan值。
Duration:获取TimeSpan的绝对值。
Hours:返回用小时计算的TimeSpan值
Milliseconds:返回用毫秒计算的TimeSpan值。
Minutes:返回用分钟计算的TimeSpan值。
Negate:返回当前实例的相反数。
Seconds:返回用秒计算的TimeSpan值。
Subtract:从中减去另一个TimeSpan值。
Ticks:返回TimeSpan值的tick数。
TotalDays:返回TimeSpan值表示的天数。
TotalHours:返回TimeSpan值表示的小时数。
TotalMilliseconds:返回TimeSpan值表示的毫秒数。
TotalMinutes:返回TimeSpan值表示的分钟数。
TotalSeconds:返回TimeSpan值表示的秒数。

          /// <summary>
          /// 日期比较
          /// </summary>
          /// <param name="today">当前日期</param>
          /// <param name="writeDate">输入日期</param>
          /// <param name="n">比较天数</param>
          /// <returns>大于天数返回true,小于返回false</returns>
          private bool CompareDate(string today, string writeDate, int n)
          {
              DateTime Today = Convert.ToDateTime(today);
              DateTime WriteDate = Convert.ToDateTime(writeDate);
              WriteDate = WriteDate.AddDays(n);
              if (Today >= WriteDate)
                  return false;
              else
                  return true;
          }