Click here to Skip to main content
16,012,061 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am uploading records from Excel file to Database. My uploaded data contains Date value. I want to check whether the uploaded date is in dd.MM.yyyy format or not. How can i do this? I have tried DateTime.TryParse() and DateTime.TryParseExact() functions, but it returns false.
My code is :

C#
DateTime date;

        if (dr["From_Date"].ToString() != "")
        {
            if (DateTime.TryParseExact(dr["From_Date"].ToString(), "dd.MM.yyyy", null, DateTimeStyles.None, out date))
            {
                return "Please specify \"From_Date\" in dd.mm.yyyy format.";
            }
        }
Posted
Updated 8-Mar-13 19:46pm
v2
Comments
Sergey Alexandrovich Kryukov 9-Mar-13 1:51am    
You need to check up first argument under the debugger. Is this string really formatted as "dd.MM.yyyy". Show the example of that string if you are in doubt.
—SA
Mit Mehta 10-Mar-13 23:21pm    
yes my string is in dd.MM.yyyy format. Here are some sample dates :
01.01.2010
20.08.2012

It should be like this :-)
C#
...
if (!DateTime.TryParseExact(dr[...
 
Share this answer
 
v2
I guess the dates are not in the exact format.
Thus you are getting false.
 
Share this answer
 
Comments
Mit Mehta 10-Mar-13 23:22pm    
Dates are in correct format. Here are some samples:
01.01.2010
20.08.2012

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