比较C#中的两个日期并计算差异

时间:2014-12-20 12:12:24

标签: c# date datetime compare

我有两个日期首先是nowDate {12/20/2014 5:19:31 PM},第二个是messageTime {12/20/2014 1:14:12 PM}我想比较这些日期并计算结果并显示给用户,它计算现在时间和消息日期之间的时差我的代码是

// Last Message Time
DateTime messageTime = Convert.ToDateTime(lastMessageTime);

int msgYear = messageTime.Year;
int msgMonth = messageTime.Month;
int msgDay = messageTime.Day;
int msgHour = messageTime.Hour;
int msgMinute = messageTime.Minute;

//Current Date Time
DateTime nowDate = DateTime.Now;

// TimeSpan difference = nowDate.Subtract(messageTime);
int nowYear = nowDate.Year;
int nowMonth = nowDate.Month;
int nowDay = nowDate.Day;
int nowHour = nowDate.Hour;
int nowMinute = nowDate.Minute;

if (msgYear == nowYear)
{
    if (msgMonth == nowMonth)
    {
        if (msgDay == nowDay)
        {
            if (msgHour == nowHour)
            {
                if (msgMinute == nowMinute)
                {
                    lastMessageTime = "Just Now";
                }
                else
                {
                    if (nowMinute - msgMinute == 1)
                        lastMessageTime = "1 minute ago";
                    else
                        lastMessageTime = nowMinute - msgMinute + " minutes ago";
                }
            }
            else
            {
                if (nowHour - msgHour == 1)
                   lastMessageTime = "1 hour ago";
                else
                   lastMessageTime = nowHour - msgHour + " hours ago";
            }
        }
        else
        {
            if (nowDay - msgDay == 1)
               lastMessageTime = "1 Day ago";
            else
               lastMessageTime = nowDay - msgDay + " days ago";
        }
    }
    else
    {
        if (nowMonth - msgMonth == 1)
           lastMessageTime = "1 month ago";
        else
           lastMessageTime = nowMonth - msgMonth + " months ago";
    }
}
else
{
    if (nowYear - msgYear == 1)
       lastMessageTime = "1 year ago";
    else
       lastMessageTime = nowYear - msgYear + " years ago";
}

但这段代码存在问题。假设上次消息时间为12/20/2014 05:59:31 PM且当前时间为12/20/2014 06:00:31 PM,则显示结果为1 hour ago,但实际差异为1分钟,请帮助某人

3 个答案:

答案 0 :(得分:7)

您的代码似乎很复杂。

如果您想比较DateTime,则可以使用<>运算符。您可以使用-运算符来区分它们。

DateTime messageTime = Convert.ToDateTime(lastMessageTime);
DateTime nowDate = DateTime.Now;
TimeSpan diff;

if(messageTime > nowDate)
   diff = messageTime - nowDate;
if(messageTime < nowDate)
   diff = nowDate - messageTime ;

你可以使用TimeSpan properties来获得分钟,小时,天等的差异。

Console.WriteLine (diff.Minutes);
Console.WriteLine (diff.Hours);
Console.WriteLine (diff.Days);

还相关:Calculate relative time in C#

答案 1 :(得分:0)

您可以编写自己的代码,但请查看人工控制器:http://www.hanselman.com/blog/NuGetPackageOfTheWeekHumanizerMakesNETDataTypesMoreHuman.aspx

要自己动手,你应该使用TimeSpan类。当您添加或减去两个日期时,您会看到TimeSpan看到Soner提供的示例

答案 2 :(得分:0)

我有答案 :)

 private void getDifference_Click(object sender, EventArgs e)
    {
        DateTime messageTime = Convert.ToDateTime("02/20/2013 6:21:12 PM");
        DateTime nowDate = DateTime.Now;
        TimeSpan diff = TimeSpan.Zero;

        if (messageTime > nowDate)
            diff = messageTime - nowDate;
        if (messageTime < nowDate)
            diff = nowDate - messageTime;

        int intSeconds = diff.Seconds;
        int intMinutes = diff.Minutes;
        int intDays = diff.Days;
        int intMonths = 0;
        int intYear = 0;
        if (intDays > 31)
        {
            intMonths = MonthDifference(nowDate, messageTime);
        }
        if (intMonths > 12)
        {
            string strYear = Convert.ToString(intMonths / 12);
            string[] strParts = strYear.Split('.');
            intYear = Convert.ToInt32(strParts[0].ToString());
        }
        string lastMessageTime = "";
        if (intYear == 0)
        {
            if (intMonths == 0)
            {
                if (intDays == 0)
                {
                    if (intMinutes == 0)
                    {
                        lastMessageTime = "just now";
                    }
                    else
                    {
                        if (intMinutes == 1)
                            lastMessageTime = "1 minute ago";
                        else
                            lastMessageTime = intMinutes + " minutes ago";
                    }
                }
                else
                {
                    if (intDays == 1)
                        lastMessageTime = "1 day ago";
                    else
                        lastMessageTime = intDays + " days ago";
                }
            }
            else
            {
                if (intMonths == 1)
                    lastMessageTime = "1 month ago";
                else
                    lastMessageTime = intMonths + " months ago";
            }
        }
        else
        {
            if (intYear == 1)
                lastMessageTime = "1 year ago";
            else
                lastMessageTime = intYear + " years ago";
        }


    }
    public int MonthDifference(DateTime nowDate, DateTime messageTime)
    {
        return (nowDate.Month - messageTime.Month) + 12 * (nowDate.Year - messageTime.Year);
    }