Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My requirement was to write a code that can accept two type of date format i.e yyyymmddhhmmss and yyyymmdd. I have written the code but it is working for few date but not for all.

C#
string[] formats = { "yyyymmdd","yyyymmddhhmmss" };
            bool valid = true;

C#
if (!DateTime.TryParseExact(DesiredReportVersionDate, formats, new CultureInfo("en-US"), DateTimeStyles.None, out timevalue))
            {

                valid = false;
                throw new Exception("invalid format" + timevalue);
            }

However,passing 19901212033047 value is giving exception.It should be valid as per my understanding.

Please help.

Thanks in advance
Posted

The problem should be that you using m (small m) for month and minute also.
It should be M (capital M) for month and m (small m) for minute...
C#
string[] formats = { "yyyyMMdd","yyyyMMddhhmmss" };
 
Share this answer
 
Comments
vicvis 1-Jul-14 9:55am    
Thanks sir.
Kornfeld Eliyahu Peter 1-Jul-14 9:56am    
Glad to help...
Thomas Daniels 1-Jul-14 9:56am    
+5!
Kornfeld Eliyahu Peter 1-Jul-14 9:57am    
Thank you...
Change the format strings from

C#
string[] formats = { "yyyymmdd","yyyymmddhhmmss" };
            bool valid = true;



to

C#
string[] formats = { "yyyyMMdd","yyyyMMddhhmmss" };
            bool valid = true;


MM = month
mm = minutes.

use HH instead of hh if you want 24 hour time.
 
Share this answer
 
Comments
Thomas Daniels 1-Jul-14 9:56am    
+5!
vicvis 1-Jul-14 10:05am    
Thanks a lot
I got this from some help.

use this yyyyMMddHHmmss. Because it's case sensitive

Thanks
 
Share this answer
 

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