Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two dates in format of dd/mm/yyyy or dd/MM/yyyy. I want to compare both date but when I am using convert.todatetime(), its not giving me proper solution.
There is any one have any idea about this comparisions.

Please reply soon
Posted
Comments
I.explore.code 17-Oct-12 5:45am    
Can you post your code please? And you don't use "mm" in date, you use "MM". "mm" = minutes, "MM"= month.
Member 8821727_GhostAnswer 17-Oct-12 6:23am    
I am comparing like this
if (Convert.ToDateTime(toDatePicker.Text) >= Convert.ToDateTime(fromDatePicker.Text))

date format of both is dd/MM/yyyy.

A simple DateTime.Compare(date1,date2) should work.

http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx[^]
 
Share this answer
 
Comments
AshishChaudha 17-Oct-12 6:00am    
my +5
I.explore.code 17-Oct-12 8:12am    
There u go! 5!
You have to use DateTime.TryParseExact if you are using custom date time format.

First,
C#
using System.Globalization;

C#
DateTimeFormatInfo dateFormat = new DateTimeFormatInfo();
dateFormat.DateSeparator = "/";

string input1 = "18/10/2012";
string input2 = "17/10/2012";

DateTime date1 = DateTime.MinValue;
DateTime date2 = DateTime.MinValue;

if (DateTime.TryParseExact(input1, "dd/MM/yyyy", dateFormat, DateTimeStyles.AllowWhiteSpaces, out date1))
{
    // Date 1 is a valid date
}
else
{
    throw new Exception("Input 1 is not a valid recognised date");
}

if (DateTime.TryParseExact(input2, "dd/MM/yyyy", dateFormat, DateTimeStyles.AllowWhiteSpaces, out date2))
{
    // Date 2 is a valid date
}
else
{
    throw new Exception("Input 2 is not a valid recognised date");
}

if (date1 > date2)
{
    // Do something
}
else
{
    // Do something
}
 
Share this answer
 
Comments
Member 8821727_GhostAnswer 18-Oct-12 6:39am    
thanks for it. I tell you after using this.

        string strFromDate = "26/03/2012";
        string strToDate = "26/04/2012";
 
        DateTime FromDate = DateTime.ParseExact(strFromDate, "dd/MM/yyyy",null);
        DateTime ToDate = DateTime.ParseExact(strToDate, "dd/MM/yyyy", null);
 
        int result = DateTime.Compare(FromDate, ToDate);
 
        if (result < 0)
        {
            //from date is less than to date
            
        }
 
Share this answer
 
v3
Comments
Vinay Raj K 18-Oct-12 6:07am    
Hope you got it...
Member 8821727_GhostAnswer 18-Oct-12 6:38am    
thanks for it. I tell you after using this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900