Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

This is about checking the Date selected in the field is equal or greater than (=>)to current date. So i have the field ID (00001234). Now i have to write an if condition i think..... Can anyone help me till the If condition, i will take it forward from there as i am very new and i am little confused with Dateformats.

Thanks in advance.

What I have tried:

I tried using datetime.now but failed to write proper if condition.
Posted
Updated 1-Jun-16 21:04pm
Comments
George Jonsson 2-Jun-16 2:53am    
You need to explain a little bit more.
How do you select the date? With a date time picker?
And what is the field? A text box, a cell in a grid or what?
Zafar Sultan 2-Jun-16 2:56am    
"failed to write proper if condition" is itself a condition worth worrying!

1 solution

Ideally when you compare dates you should have both the dates in same format otherwise you will end up with weird output. This is a small snippet for date comparison.

C#
public void CompareDates()
        {
            string inputDate = "05/21/2016";
            DateTime convertedDate = DateTime.Parse(inputDate);//will have time part as well.
            DateTime nowDate = DateTime.Now;

            //The following code will consider time part in date.
            if (nowDate >= convertedDate)
            {
                //nowDate is greater or equal
            }
            else
            {
                //convertedDate is greater
            }

            //The following code will consider only date(no time)
            if (nowDate.Date >= convertedDate.Date)
            {
                //nowDate is greater or equal
            }
            else
            {
                //convertedDate is greater
            }
        }

Put a debugger at the very beginning of the code and see what the compiler gives you on each line. For different date formats please refer this[^] and this[^] links
 
Share this answer
 
v2
Comments
Member 8010354 2-Jun-16 4:11am    
I tried using DateTime nowdate = DateTime.Now.ToString("dd/MM/yyyy h:mm tt");

but it giving error as it couldn't the format.
But when i take the string
string currenttime =datetime.now.tostring("dd/MM/yyyy h:mm t");
Now how to use this string in the program?
George Jonsson 2-Jun-16 5:20am    
You got it a bit backwards. Of course you cannot do DateTime nowdate = DateTime.Now.ToString("dd/MM/yyyy h:mm tt"); DateTime is one type and string is another type.
If you want to compare two dates and your input date is a string, then first convert the string to a date.
Use DateTime.Parse() or DateTime.ParseExact()
Now really look at the given solution and try it out.

And we cannot know how to use it in your program, because you don't present any code.
George Jonsson 2-Jun-16 5:19am    
Use DateTime.Parse() or DateTime.ParseExact instead. It gives you better control of the conversion.

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