Solved

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

Posted on 2010-09-02
7
243 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 85

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:ewangoya
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 25
SQL Help 27 45
VB.Net - MemoryMappedFiles - Confirm receipt 2 19
Sending receiving text messages in vb.net 15 36
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

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