How do you write code to compare date in C#?



  • Java code look like this.

     public static void main( String[] args ) 
        {
        	try{
    
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        	Date date1 = sdf.parse("2009-12-31");
        	Date date2 = sdf.parse("2010-01-31");
    
        	System.out.println(sdf.format(date1));
        	System.out.println(sdf.format(date2));
    
        	if(date1.after(date2)){
        		System.out.println("Date1 is after Date2");
        	}
    
        	if(date1.before(date2)){
        		System.out.println("Date1 is before Date2");
        	}
    
        	if(date1.equals(date2)){
        		System.out.println("Date1 is equal Date2");
        	}
    
    	}catch(ParseException ex){
    		ex.printStackTrace();
    	}
    

    C# code is looking like this

    DateTime date1  = new DateTime(2009,12,31);
    DateTime date2  = new DateTime(2010,01,31);
    

    if (date1 < date2)
    {
    Console.WriteLine("Date2 greater " + date2.Date);
    }
    else if (date1 > date2)
    {
    Console.WriteLine("Date1 higher! " + date1.Date);
    }
    else
    {
    Console.WriteLine("Equal Dates");
    }




    Both code working fine, but is there better way to compare date in C#?



  •  

    DateTime date1  = new DateTime(2009,12,31);
    DateTime date2  = new DateTime(2010,01,31);
    

    if (date1 > date2)
    {
    Console.WriteLine("Date1 higher! " + date1.Date);
    }
    else
    {
    if ( date1 < date2)
    {
    Console.WriteLine("Date2 higher! " + date2.Date);
    }
    else
    {
    Console.WriteLine("Equal Dates");
    }
    }

    Just to be accurate in the feedback to the user.

    I don't know much
    about C# or the use of this snippet so don't take my word as Divine
    Truth, it's just a nudge in the right direction.





  • <, >, ==, would be the preferred ways to compare dates.





  • @Nagesh said:

    @Cassidy said:

    I'm no C# coder, but I would imagine - just like Java - there's some comparator that does the job for you.

    That look much better

    Thx.

    Yes, because

    int compareValue = date1.CompareTo(date2)

    if(compareValue > 0) {}

    else if(compareValue < 0) {}

    else {}

     

    is much easier to read than

    if(date1 > date2) {}

    else if(date1 < date2) {}

    else {}

     

    *roll*



  • Your sarcasm like behaviour is not bother me at all.



  • All your sarcasm is not bother to you?



  • All your sarcasm, sarcasm, sarcasm.... All your sarcasm, is not bother to me.



  • You have no chance to repent! Make your time!



  • You could use Compare API of datetime object to get this done.

    DateTime date1 = DateTime.parse("2011-03-01")

    DateTime date2 = DateTime.parse("2012-03-01")

     int result = DateTime.Compare(date1, date2);

    if (result < 0)
                    Console.writeLine("date1 is earlier than date2)";
                else if (result == 0)
                    Console.writeLine("date1 is equal to date2)";
                else
                    Console.writeLine("date1 is later than date2)";

    <font color="#0000ff" size="2"><font color="#0000ff" size="2">

    if you want to parse in various date formats or if you want more detailed explanation you could go to http://technico.qnownow.com/2012/03/20/how-to-compare-dates-in-c/

    </font></font>

    <font size="2"></font>

    <font size="2">

    </font>

    <font size="2"></font>

    <font size="2"></font> 

    <font size="2"></font>



  • @veeraC85 said:

    You could use Compare API of datetime object to get this done.

    DateTime date1 = DateTime.parse("2011-03-01")

    DateTime date2 = DateTime.parse("2012-03-01")

     int result = DateTime.Compare(date1, date2);

    if (result < 0)
                    Console.writeLine("date1 is earlier than date2)";
                else if (result == 0)
                    Console.writeLine("date1 is equal to date2)";
                else
                    Console.writeLine("date1 is later than date2)";

    <font size="2"></font> 

    <font size="2"></font>
    Did you create an account JUST to post something that's already been posted in the thread?  That's not even spambot, that's... just... i don't know.



  • In the my opinion, there are some suitable way to compare the data in the C#. Some are defined here

    1- To compare the data with each other ,we use the '==' and 'equals' method

    2- To compare the data type of the variable of the each other we use the '===' .

    etc.

                I hope that if you finds any problem in your c# code then you can mail me at "passcertification@ymail.com" . I am waiting you action.

    Thanking you.



  • The funniest thing is that "passcertification@ymail.com" will probably start receiving tons of spam.



  • @lisawinkler said:

    2- To compare the data type of the variable of the each other we use the '===' .

    Umm, C# doesn't have a === operator... you thinking of Javascript?



  • @ekolis said:

    you thinking of Javascript?

    I'm thinking "do not feed the bots"...



  • @Nagesh said:

    Java code look like this.

     public static void main( String[ args ) 
        {
        	try{
     
        		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            	Date date1 = sdf.parse("2009-12-31");
            	Date date2 = sdf.parse("2010-01-31");
     
            	System.out.println(sdf.format(date1));
            	System.out.println(sdf.format(date2));
     
            	if(date1.after(date2)){
            		System.out.println("Date1 is after Date2");
            	}
     
            	if(date1.before(date2)){
            		System.out.println("Date1 is before Date2");
            	}
     
            	if(date1.equals(date2)){
            		System.out.println("Date1 is equal Date2");
            	}
     
        	}catch(ParseException ex){
        		ex.printStackTrace();
        	}
    

    C# code is looking like this

    DateTime date1  = new DateTime(2009,12,31);
    DateTime date2  = new DateTime(2010,01,31);
    

    if (date1 date2)
    {
    Console.WriteLine("Date1 higher! " + date1.Date);
    }
    else
    {
    Console.WriteLine("Equal Dates");
    }




    Both code working fine, but is there better way to compare date in C#?

    Simple way you can do this

    TimeSpan diff = date2.Subtract(date1);

    more info :

    http://csharp.net-informations.com/statements/csharp-date-difference.htm

     

    eldo.

     

     



  • @Nagesh said:

    if(date1.equals(date2)){ System.out.println("Date1 is equal Date2"); }
     

    Given you've tested for greater and less... is there any point in performing a third test?



  • @Cassidy said:

    @Nagesh said:

    if(date1.equals(date2)){
    System.out.println("Date1 is equal Date2");
    }
     

    Given you've tested for greater and less... is there any point in performing a third test?

    Above example for illustrating purpose only. What you're are saying is it is necessary to write water-tight code.



  • @Nagesh said:

    What you're are saying is it is necessary to write water-tight code.
     

    No, I'm saying it's pointless testing for a situation that must hold true in order to reach that part of the code.


Log in to reply