Shyju
Shyju

Reputation: 218732

C# , How can i build a DateTime object from a datetime present in a string object?

in C# , How can i build a DateTime object from a datetime present in a string object ( String strDate="11/11/2009")?

Upvotes: 1

Views: 131

Answers (4)

AdaTheDev
AdaTheDev

Reputation: 147244

There's also DateTime.ParseExact which is also worth knowing about.

There's also TryParse and TryParseExact which do the same thing, but won't throw an exception if the date is not valid - will just return a bool.

Upvotes: 3

Adriaan Stander
Adriaan Stander

Reputation: 166396

Using that string format will give you a lot of issues later, such as what is "03/04/2009"?

try using DateTime.ParseExact.

Upvotes: 1

softveda
softveda

Reputation: 11066

Use DateTime.Parse

Upvotes: 1

Konrad Rudolph
Konrad Rudolph

Reputation: 545608

Have a look at the DateTime.Parse or DateTime.ParseExact methods.

Simplest case:

var result = DateTime.Parse(str);

Upvotes: 6

Related Questions