Solved

How do I compare dates to see if one is greater or equal to another (C#)

Posted on 2010-09-02
7
241 Views
Last Modified: 2013-12-17
This is what I have.  What is wrong with my code?

        string c1 = TextBox1.Text;
        string c2 = TextBox2.Text;

          if (DateTime.Compare(Convert.ToDateTime(c1),Convert.ToDateTime(c2) < 0))
        {
            Label1.Text = "Is greater";
        }
0
Comment
Question by:bmanmike39
7 Comments
 
LVL 1

Expert Comment

by:phibersg
Comment Utility
if (Convert.ToDateTime(txtIn.Text) > Convert.ToDateTime("1/1/2000"))

        string c1 = TextBox1.Text;
        string c2 = TextBox2.Text;

          if (DateTime.Compare(Convert.ToDateTime(c1),Convert.ToDateTime(c2) < 0))
if (Convert.ToDateTime(txtIn.Text) > Convert.ToDateTime("1/1/2000"))
        {
            Label1.Text = "Is greater";
        }
0
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
Comment Utility
Hi,

Try

        string c1 = TextBox1.Text;
        string c2 = TextBox2.Text;

          if (DateTime.Compare(Convert.ToDateTime(c1),Convert.ToDateTime(c2)) < 0)
        {
            Label1.Text = "Is greater";
        }

/peter
0
 
LVL 1

Expert Comment

by:phibersg
Comment Utility
Sorry first post was a misclick on submit..

        string c1 = TextBox1.Text;
        string c2 = TextBox2.Text;

        if (Convert.ToDateTime(c1) > Convert.ToDateTime(c2))
        {
            Label1.Text = "Is greater";
        }
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Expert Comment

by:phibersg
Comment Utility
awww damn it.. i must be drunk..

yeah.. this is the correct line..

" if (DateTime.Compare(Convert.ToDateTime(c1),Convert.ToDateTime(c2) < 0)) "
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
See code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DateTime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = System.DateTime.Now.ToString();
            textBox2.Text = System.DateTime.Now.AddMinutes(60).ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string c1 = textBox1.Text;
            string c2 = textBox2.Text;

            if (System.DateTime.Compare(Convert.ToDateTime(c2), Convert.ToDateTime(c1)) >= 0)
            {
                Label1.Text = "Is greater";
            }
        }
    }
}

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
I would do:
        private void button1_Click(object sender, EventArgs e)

        {            

            DateTime dt1, dt2;

            if (DateTime.TryParse(textBox1.Text, out dt1) )

            {

                if (DateTime.TryParse(textBox2.Text, out dt2))

                {

                    if (dt1 == dt2)

                    {

                        label1.Text = "Dates are equal";

                    }

                    else if (dt1 > dt2)

                    {

                        label1.Text = "Date #1 > Date #2";

                    }

                    else

                    {

                        label1.Text = "Date #1 < Date #2";

                    }

                }

                else

                {

                    label1.Text = "Invalid Date in #2";

                }

            }

            else

            {

                label1.Text = "Invalid Date in #1";

            }

        }

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility
private void button1_Click(object sender, EventArgs e)
        {
            DateTime Date1 = Convert.ToDateTime(textBox1.Text);
            DateTime Date2 = Convert.ToDateTime(textBox2.Text);

            if (DateTime.Compare(Date1, Date2) > 0)
            {
                label1.Text = "Date1 is greater";
            }
        }
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now