0

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.

string[] formats = { "yyyymmdd","yyyymmddhhmmss" };

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

vish1990
  • 272
  • 3
  • 16
  • 2
    http://www.mikesdotnetting.com/Article/23/Date-Formatting-in-CSharp Care about those M and m ('s) – Kilazur Jul 01 '14 at 13:47
  • Or indeed MSDN: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx Always read the documentation. – Jon Skeet Jul 01 '14 at 13:48

2 Answers2

4

use this yyyyMMddHHmmss. Because it's case sensitive

Nadendla
  • 712
  • 2
  • 7
  • 17
0

Capital letters matter. In your case use capital MM instead of mm.

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68