?
Solved

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

Posted on 2010-09-02
7
Medium Priority
?
247 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 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

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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 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