Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

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

Posted on 2010-09-02
Medium Priority
249 Views
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
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

LVL 1

Expert Comment

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

pivar earned 2000 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

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

LVL 1

Expert Comment

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

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();
}

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";
}
}
}
}
``````
0

LVL 86

Expert Comment

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";
}
}
``````
0

LVL 32

Expert Comment

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

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeâ€¦
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for â€¦
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrâ€¦
###### Suggested Courses
Course of the Month10 days, 4 hours left to enroll