converting string to 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
 
J

Jon Skeet [C# MVP]

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.
 
D

Daniel Patriak

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.
 
J

Jon Skeet [C# MVP]

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.
 
D

Daniel Patriak

Ok, that works. BIG THANKS. I've lost much time for searching.

Greetings from Poland!!!
 
M

Miha Markic [MVP C#]

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top