Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 ASP.NET DateTime , +
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 17-Oct-12 0:40am
Comments
Aman.A at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

A simple DateTime.Compare(date1,date2) should work.
 
http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx[^]
  Permalink  
Comments
AshishChaudha at 17-Oct-12 6:00am
   
my +5
Aman.A at 17-Oct-12 8:12am
   
There u go! 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You have to use DateTime.TryParseExact if you are using custom date time format.
 
First,
using System.Globalization;
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
}
  Permalink  
Comments
Member 8821727_GhostAnswer at 18-Oct-12 6:39am
   
thanks for it. I tell you after using this.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3


        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
            
        }
  Permalink  
v3
Comments
VinayRajK at 18-Oct-12 6:07am
   
Hope you got it...
Member 8821727_GhostAnswer at 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)

  Print Answers RSS
0 OriginalGriff 280
1 Jochen Arndt 150
2 Kornfeld Eliyahu Peter 120
3 PIEBALDconsult 110
4 Richard MacCutchan 105
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 18 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100