Take the 2-minute tour ×

I am unable to convert strings to date with throwing exceptions of not proper format:

here are the patterens:


formats are coming from dropdown list and here is what I tried different method but still its throwing exceptions:

 strCurrentFormat = rcboDateFormat.SelectedValue.ToString();

                 DateTime db = DateTime.ParseExact(strOldDate, "DD-MM-YYYY", CultureInfo.InvariantCulture);

                 //DateTime loadedDate = DateTime.ParseExact(strOldDate, strCurrentFormat, null);

I have solved this problem before by taking string, split them and move the year and month and day around to proper format but that would take long time, if anyone knows an easier way it would be a tremendous help.

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

Two things:

First - use the correct format strings. D and Y are not known format specifiers. d and y are.

Second - you can put your formats into a string array and use the ParseExact overload that takes that list of formats.

string[] formats = new string[] 

DateTime db = DateTime.ParseExact(strOldDate, 

The formats will be attempted in order.

share|improve this answer
Thanks worked perfectly –  Developer Jun 26 '12 at 19:35
@JonSenchyna - It will try in each format in succession till it succeeds. The main issue for the OP was that the format strings were using incorrect format specifiers. –  Oded Jun 26 '12 at 20:00
add comment

Your format string is wrong.

You need lowercase for "d" and "y", so


Read all about the format string on MSDN.

share|improve this answer
add comment

Taken from http://www.codeproject.com/Articles/14743/Easy-String-to-DateTime-DateTime-to-String-and-For

d - Numeric day of the month without a leading zero. dd - Numeric day of the month with a leading zero. ddd - Abbreviated name of the day of the week. dddd - Full name of the day of the week.

f,ff,fff,ffff,fffff,ffffff,fffffff - Fraction of a second. The more Fs the higher the precision.

h - 12 Hour clock, no leading zero. hh - 12 Hour clock with leading zero. H - 24 Hour clock, no leading zero. HH - 24 Hour clock with leading zero.

m - Minutes with no leading zero. mm - Minutes with leading zero.

M - Numeric month with no leading zero. MM - Numeric month with a leading zero. MMM - Abbreviated name of month. MMMM - Full month name.

s - Seconds with no leading zero. ss - Seconds with leading zero.

t - AM/PM but only the first letter. tt - AM/PM ( a.m. / p.m.)

y - Year with out century and leading zero. yy - Year with out century, with leading zero. yyyy - Year with century.

zz - Time zone off set with +/-.

share|improve this answer
add comment

Your Answer


