c# - Particular DateTime exception -


i need convert datetime strings format "mmm-yy". i'm working culture "{es-es}".

it works fine month except march (in spanish marzo). throws me exception:

'convert.todatetime("mar-13")' threw exception of type 'system.formatexception' system.datetime {system.formatexception}

i've tried:

string format = "yyyymm"; datetime result; cultureinfo provider = cultureinfo.invariantculture; result = datetime.parseexact("mar-13", format, provider); 

and this:

datetime date = convert.todatetime("mar-13"); 

this works fine example with:

"jun-13"

"feb-13"

"nov-13"

...

edit real problem with:

datetime date = convert.todatetime("ene-13"); -> ok

datetime date = convert.todatetime("feb-13"); -> ok

datetime date = convert.todatetime("mar-13"); -> crash

datetime date = convert.todatetime("abr-13"); -> ok

....

your date string "mar-13" doesn't match format "yyyymm". format should mmm-yy.

you should see: custom date , time format strings

in format

"mmm" - abbreviated name of month.

"yy" - year, 00 99.

edit:

for question why convert.todatetime("mar-13"); failing. need @ following lines of code:

var currentculture = new cultureinfo("es-es"); var monthnames = currentculture.datetimeformat.abbreviatedmonthnames; var dayofweeks = currentculture.datetimeformat.abbreviateddaynames; 

if watch returned values in debugger, see culture es-es there match between month name , day name , on mar.

  • marzo/march month
  • martes/tuesday day

both of these uses same abbreviation i.e. mar. since convert.todatetime try use possible formats string fails recognize mar month or day name. why exception.

it idea use datetime.parseexact , specify single or multiple possible formats.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -