• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1507
  • Last Modified:

Datetime validation in c# issue

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
madhanmss
Asked:
madhanmss
  • 2
1 Solution
 
philipjonathanCommented:
Try changing the hh to HH:
ValidateDateTime("131212", "HHmmss")
0
 
anarki_jimbelCommented:
Try "HHmmss" formal, or "Hmmss" for time. Lower case "hh" is probably used for am/pm format like "hh:mm tt"
0
 
anarki_jimbelCommented:
Ha-ha, 3 minutes late! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now