WEB开发者-分享WEB开发知识,让开发变得更简单!

c#中返回文章发表的时间差的示例

代码如下:

 public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
         {
             string dateDiff = null;
             int date = 0;
             try
             {
                 int year = Convert.ToInt32(DateTime2.ToString("yyyy")) - Convert.ToInt32(DateTime1.ToString("yyyy"));
                 TimeSpan ts = DateTime2 - DateTime1;
                 string t1 = DateTime1.ToString("yyyy-MM-dd");
                 string t2 = DateTime2.ToString("yyyy-MM-dd");
                 DateTime d1 = DateTime.Parse(t1);
                 DateTime d2 = DateTime.Parse(t2);
                 TimeSpan ts2 = d2 - d1;
                 date = ts2.Days;
                 if (date == 0)
                 {
                     if (ts.Hours > 1)
                     {
                         dateDiff = ts.Hours.ToString() + "小时前";
                     }
                     else
                     {
                         if (ts.Minutes > 1)
                         {
                             dateDiff = ts.Minutes.ToString() + "分钟前";
                         }
                         else
                         {
                             dateDiff = ts.Seconds.ToString() + "秒前";
                         }
                     }
                 }
                 else
                 {
                     if (date == 1)
                     {
                         dateDiff = "昨天";
                     }
                     else
                     {
                         if (date == 2)
                         {
                             dateDiff = "前天";
                         }
                         else
                         {
                             if (year == 0)
                             {
                                 dateDiff = DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日";
                             }
                             else
                             {
                                 dateDiff = DateTime1.Year.ToString() + "年" + DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日";
                             }
                         }
                     }
                 }
             }
             catch
             { }
             return dateDiff;
         }          
暂无相关信息

本文链接:https://www.webkfz.com/bianchengkaifa/CJC/7943.html

版权声明:站内所有文章皆来自网络转载,只供分享作用,不代表本站的观点!

发表评论 共有 0 条评论)

联系客服
网站客服 业务合作 QQ
1244305267
公众号
公众号
公众号
返回顶部