converting string to date

  • Thread starter Thread starter Daniel Patriak
  • Start date Start date
D

Daniel Patriak

hello!

Anybody knows if there is a method in C#, that can convert date given as
string, using date format given as string (like DD/MM/YYYY, DD-MM-YYYY) ?

Please help!
I couldn't find it in MSDN!

Thanks
Daniel
 
Daniel Patriak said:
Anybody knows if there is a method in C#, that can convert date given as
string, using date format given as string (like DD/MM/YYYY, DD-MM-YYYY) ?

Look at DateTime.ParseExact.
 
Ok, but how to force this method to respect my format. There was usend
culture in example, but all I have is date format as a string. I don't know
how to create Culture using this format.
 
Daniel Patriak said:
Ok, but how to force this method to respect my format. There was usend
culture in example, but all I have is date format as a string. I don't know
how to create Culture using this format.

You can specify Culture.InvariantCulture. You specify the format as
another of the parameters.
 
Ok, that works. BIG THANKS. I've lost much time for searching.

Greetings from Poland!!!
 
Hi Daniel,

Or, if you want custom format you might do something like:
string datestr = "21/01/2004";

CultureInfo ci = new CultureInfo("sl-SI", true);

ci.DateTimeFormat.DateSeparator = "/";

ci.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";

DateTime date = DateTime.Parse(datestr, ci);
 
Back
Top