Converting string format to datetime in mm/dd/yyyy

c#, datetime

Solution

You are looking for the `DateTime.Parse()` method (MSDN Article)

So you can do:

var dateTime = DateTime.Parse("01/01/2001");

Which will give you a `DateTime` typed object.

If you need to specify which date format you want to use, you would use `DateTime.ParseExact` (MSDN Article)

Which you would use in a situation like this (Where you are using a British style date format):

string[] formats= { "dd/MM/yyyy" }
var dateTime = DateTime.ParseExact("01/01/2001", formats, new CultureInfo("en-US"), DateTimeStyles.None);

Problem

I have to convert string in mm/dd/yyyy format to datetime variable but it should remain in mm/dd/yyyy format. ``` string strDate = DateTime.Now.ToString("MM/dd/yyyy"); ``` Please help.

Original source