Elena
Elena

Reputation: 839

DateTime formats C#

I have a string which needs to be converted and validated to a DateTime. The string is in the following format 'dd.mm.yy' I am trying to convert it to DateTime using the following

string format = "dd.mm.yy";
date = DateTime.ParseExact(current.Substring(aiRule.AiLength), format, 
                                           CultureInfo.InvariantCulture);

but unfortunately this fails. The question is how to convert a string in the format 'dd.mm.yy' to a DateTime ? Thank you

Upvotes: 1

Views: 199

Answers (4)

xanatos
xanatos

Reputation: 111810

I'll tell something "heretical". If dd.MM.yy (with 2 or 4 yy) is the format of your local culture, then you could let the DateTime.Parse (not ParseExact!) do its work without setting it to CultureInfo.InvariantCulture, or perhaps setting it to your local culture like new CultureInfo("it-IT").

Upvotes: 0

Christoffer
Christoffer

Reputation: 2125

As earlier posts has already pointed out, mm means minutes and MM means months. I ran this test snippet and it works as expected:

string format = "dd.MM.yy";
string date = "27.10.11";
DateTime result;
result = DateTime.ParseExact(date, format, CultureInfo.InvariantCulture);

Upvotes: 0

rockyashkumar
rockyashkumar

Reputation: 1332

the string format should be like this....

string Format =  "dd.MM.yy"


 mm is for showing minutes 

 MM is for showing months..

I hope it will helps you...

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

mm means "minutes". I suspect you want "dd.MM.yy". See MSDN for more information about custom date and time format strings.

(In particular, read the part about the "yy" specifier and how it chooses which century to use. If you can possibly change the input to use a four digit year, that could save you some problems...)

Upvotes: 11

Related Questions