[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Datetime validation in c# issue

Posted on 2008-11-18
3
Medium Priority
?
1,503 Views
Last Modified: 2013-12-17
Hi, I need to validate the date and time part separately. I am using the following function to determine the correct date and time.

private bool ValidateDateTime(string szDateTime, string szFormat)
        {
            try
            {
                   System.IFormatProvider format = new System.Globalization.CultureInfo("en-US", true);                  
                   return DateTime.TryParseExact(szDateTime, szFormat, format,  
                   System.Globalization.DateTimeStyles.AllowWhiteSpaces, out outDate);                
            }
            catch (Exception ex)
            {
               
            }
            return false;
        }

The inputs and validation results are given below.
1. ValidateDateTime("20081005","yyyyMMdd")  - Pass
2.ValidateDateTime("121212","hhmmss")  - Pass

But if i give the time as 13 like below, it fails.
3.ValidateDateTime("131212","hhmmss")  - Fail

Please let me know how to fix this issue as the time component can be between 0-23.

Regards,
Madhan
0
Comment
Question by:madhanmss
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
philipjonathan earned 1000 total points
ID: 22991503
Try changing the hh to HH:
ValidateDateTime("131212", "HHmmss")
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 22991514
Try "HHmmss" formal, or "Hmmss" for time. Lower case "hh" is probably used for am/pm format like "hh:mm tt"
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 22991519
Ha-ha, 3 minutes late! :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

872 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