时间差(类.精确到秒).net中实现Datediff类C#

.NET中,经常用到两个时间的比较,如,新闻列表后显示(5分钟前)要比显示(2010-2-22 12:50)这样的时间格式直观得多了。显示结果如:.net中实现Datediff类C# ,以下是详细类代码.

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 比较两个时间,返回时间间隔
/// </summary>
namespace CBF.Common
{
    public class DateTimeDiff
    {
        public DateTimeDiff()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 把秒转换成分钟
        /// </summary>
        /// <returns></returns>
        public static int SecondToMinute(int Second)
        {
            decimal mm = (decimal)((decimal)Second / (decimal)60);
            return Convert.ToInt32(Math.Ceiling(mm));
        }
        /// <summary>
        /// 计算两个时间的时间间隔
        /// </summary>
        /// <param name="DateTimeOld">较早的日期和时间</param>
        /// <param name="DateTimeNew">较后的日期和时间</param>
        /// <returns></returns>
        public static string DateDiff(DateTime DateTimeOld, DateTime DateTimeNew)
        {
            string dateDiff = "";
            TimeSpan ts1 = new TimeSpan(DateTimeOld.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTimeNew.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            int day = ts.Days;
            int hou = ts.Hours;
            int minu = ts.Minutes;
            int sec = ts.Seconds;
            if (day > 0)
            {
                if (day > 30)
                {
                    if (day > 364)
                    {
                        dateDiff += day /365 + "年";
                    }
                    else
                    {
                        dateDiff += day/30 + "个月";
                    }
                }
                else
                {
                    dateDiff += day.ToString() + "天";
                }
            }
            else
            {
                if (hou > 0)
                {
                    dateDiff += hou.ToString() + "小时";
                }
                else
                {
                    if (minu > 0)
                    {
                        dateDiff += minu.ToString() + "分钟";
                    }
                    else
                    {
                        if (sec > 0)
                        {
                            dateDiff += sec.ToString() + "秒";
                        }
                        else
                        {
                            dateDiff += "0秒";
                        }
                    }
                }
            }
            if (DateTimeNew.CompareTo(DateTimeOld)>0)
            {
                dateDiff += "前";
            }
            else
            {
                dateDiff += "后";
            }
            return dateDiff;
        }
        /// <summary>
        ///判断是否于5分钟之前
        /// </summary>
        /// <param name="DateTimeOld">较早的日期和时间</param>
        /// <returns></returns>
        public static bool DateDiff_minu(DateTime DateTimeOld)
        {
            TimeSpan ts1 = new TimeSpan(DateTimeOld.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            int minu = ts.Minutes;
            if (minu > 5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 与当前时间比较,重载时间比较函数,只有一个参数
        /// </summary>
        /// <param name="DateTimeOld">较早的日期和时间</param>
        /// <returns></returns>
        public static string DateDiff(DateTime DateTimeOld)
        {
            return DateDiff(DateTimeOld, DateTime.Now);
        }
        /// <summary>
        /// 日期比较,返回精确的几分几秒
        /// </summary>
        /// <param name="DateTime1">较早的日期和时间</param>
        /// <param name="DateTime2">较迟的日期和时间</param>
        /// <returns></returns>
        public static string DateDiff_full(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;
        }
        /// <summary>
        /// 时间比较,返回精确的几秒
        /// </summary>
        /// <param name="DateTime1">较早的日期和时间</param>
        /// <param name="DateTime2">较迟的日期和时间</param>
        /// <returns></returns>
        public static int DateDiff_Sec(DateTime DateTime1, DateTime DateTime2)
        {
            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            int dateDiff = ts.Days * 86400 + ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
            return dateDiff;
        }


        /// <summary>
        /// 日期比较
        /// </summary>
        /// <param name="today">当前日期</param>
        /// <param name="writeDate">输入日期</param>
        /// <param name="n">比较天数</param>
        /// <returns>大于天数返回true,小于返回false</returns>
        public static 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;
        }

 


       
    }
}

posted @ 2016-07-29 14:05  离。  阅读(52)  评论(0编辑  收藏  举报