Solved

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

Posted on 2010-09-02
7
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:phibersg
ID: 33589149
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
ID: 33589163
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
ID: 33589165
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 1

Expert Comment

by:phibersg
ID: 33589180
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
ID: 33589409
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 86

Expert Comment

by:Mike Tomlinson
ID: 33589816
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:Ephraim Wangoya
ID: 33589827
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

724 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