Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i have one field in design page as follows;
 
Date of Birth textbox1(calendar image)
 
i want to validate dateof birth date should be greater than the todays date and more than the todays date.
 
for that i write the code as follows;
 
DateTime DOB = Convert.ToDateTime(FromDate.ToString());
 
if(FromDate.SelectedDateValue > DateTime.Now)
{
Label6.Text = ("you have not born yet");
}
i try the above code when i run i show the error message as follows;
 
Operator '>' cannot be applied to operands of type 'string' and 'System.DateTime.
 

for the above code please correct my code and reply.
 

thanks,
Posted 25-Dec-12 22:59pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi , try this
DateTime compare;
if(!DateTime.TryParse(FromDate.SelectedDateValue, out compare))
//fail to parse data
var result =DateTime.Compare(compare,DateTime.Now);
if(resul>0)
Label6.Text = ("you have not born yet");
  Permalink  
v2
Comments
Oleksandr Kulchytskyi at 26-Dec-12 5:46am
   
Thanks :) I alway try to use TryParse methods, because it's a robust approach not so error prone like would be in case of usage Parse methods =)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try some thing like below code.
 
 if (DOB.CompareTo(DateTime.Now) > some_integer_value)
            {
                //your required message
            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You are comparing with a string! you can use DOB object itself
 
Use as below..
 
if(DOB.Date > DateTime.Now.Date) //Now holds time as well.
{
Label6.Text = ("you have not born yet");
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Try This....
 
It working....
 

 
if (DateTime.Compare(FromDate.SelectedDateValue, DateTime.Now) > 0)
            {
 
                Label6.Text ="you have not born yet";
 

            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

DateTime val;
if(!DateTime.TryParse(SelectedDate, out val))
Console.WriteLine("error");
else
if(val.Ticks - DateTime.Now.Ticks > 0)
Label6.Text = ("you have not born yet");
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 sanket saxena 355
1 Abhinav S 303
2 Sergey Alexandrovich Kryukov 299
3 OriginalGriff 225
4 thatraja 220
0 Sergey Alexandrovich Kryukov 8,497
1 OriginalGriff 4,850
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid