locked
Comparing Dates RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code . Is it correct way or we can compare dates in another way.

    DateTime m_EmploymentDate = Convert.ToDateTime(lblEmploymentDate.Text.ToString());
                        DateTime m_FixedDate = Convert.ToDateTime(txt_FixedDate.Text.ToString());
                        if (m_FixedDate <= m_EmploymentDate)
                        {
                            errMessage += "Date Should be Greater than " + m_EmploymentDate.ToShortDateString() + ". </br>";
                        }

    Thanks

    Tuesday, December 25, 2018 1:12 PM

Answers

  • User-1174608757 posted

    Hi JagjitSingh;

    According to your description ,I have made a sample here.Firstly, your code works well,you could directly compare dates by using '<'or '>',

    Secondly I suggest you to use method int result =  DateTime.Compare(date1,date2) which result<0 means date1 is earlier than date2 and result >0 means date1 is later than date2,

    Here is a work around.I hope it can help you.

    Datetime.aspx:

    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                lblEmploymentDate:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
                txt_FixedDate:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
                <asp:Button ID="But1" runat="server" Text="Compare" OnClick="But1_Click" />
            </div>
        </form>
    </body>

    Datetime.aspx.cs:

     public partial class Datetime : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void But1_Click(object sender, EventArgs e)
            {
               
                DateTime date1 = Convert.ToDateTime(TextBox1.Text);
                DateTime date2 = Convert.ToDateTime(TextBox2.Text);
    
                int result = DateTime.Compare(date1, date2);
                string s;
                if (result < 0)
                {
                    s = "is  earlier than";
    
                }
                else if (result == 0)
                {
                    s = "is the same time as ";
                   
                }
                else 
                {
                    s = "is later than";
                    
                }
                Response.Write(string.Format("{0} {1} {2}", date1,s, date2));
    
    
            }
        }

    It shows  as below:

    Best Regards

    Wei Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 26, 2018 2:14 AM
  • User-1716253493 posted

    Comparing two dates with two datetime variables is correct way

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 26, 2018 3:09 AM

All replies

  • User-1174608757 posted

    Hi JagjitSingh;

    According to your description ,I have made a sample here.Firstly, your code works well,you could directly compare dates by using '<'or '>',

    Secondly I suggest you to use method int result =  DateTime.Compare(date1,date2) which result<0 means date1 is earlier than date2 and result >0 means date1 is later than date2,

    Here is a work around.I hope it can help you.

    Datetime.aspx:

    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                lblEmploymentDate:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
                txt_FixedDate:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
                <asp:Button ID="But1" runat="server" Text="Compare" OnClick="But1_Click" />
            </div>
        </form>
    </body>

    Datetime.aspx.cs:

     public partial class Datetime : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void But1_Click(object sender, EventArgs e)
            {
               
                DateTime date1 = Convert.ToDateTime(TextBox1.Text);
                DateTime date2 = Convert.ToDateTime(TextBox2.Text);
    
                int result = DateTime.Compare(date1, date2);
                string s;
                if (result < 0)
                {
                    s = "is  earlier than";
    
                }
                else if (result == 0)
                {
                    s = "is the same time as ";
                   
                }
                else 
                {
                    s = "is later than";
                    
                }
                Response.Write(string.Format("{0} {1} {2}", date1,s, date2));
    
    
            }
        }

    It shows  as below:

    Best Regards

    Wei Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 26, 2018 2:14 AM
  • User-1716253493 posted

    Comparing two dates with two datetime variables is correct way

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 26, 2018 3:09 AM