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
7
Medium Priority
?
249 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…

609 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