Solved

C# valid date entry

Posted on 2008-10-06
5
577 Views
Last Modified: 2013-12-17
Function returns false though valid date is entered.

Date Entered : 06/31/2008
 private static bool IsDate(string sDate)
        {
            DateTime dt;
            bool isDate = true;

            try
            {
                dt = DateTime.Parse(sDate);
            }
            catch
            {
                isDate = false;
            }

            return isDate;
        }
0
Comment
Question by:dotnet0824
  • 2
  • 2
5 Comments
 
LVL 14

Assisted Solution

by:Binuth
Binuth earned 25 total points
ID: 22648996
june has only 30 days
0
 
LVL 14

Expert Comment

by:Binuth
ID: 22648999
:)
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 75 total points
ID: 22649134
to avoid problems with regional settings in different machines, I suggest to use ParseExact:

DateTime.ParseExact(sDate, "d", CultureInfo.InvariantCultureInfo);
0
 
LVL 13

Assisted Solution

by:crazyman
crazyman earned 25 total points
ID: 22649144
Lose the try catch and use DateTime.TryParseExact
0
 
LVL 13

Expert Comment

by:crazyman
ID: 22649157
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question