C# Convert String to DateTime

Updated on 21 May 2012,
Published on 20 Feb 2008

To convert string into DateTime you can use Convert.ToDateTime function in C# ASP.Net 2.0. Further you can also format the output of Date or Time.

DateTime Format Examples:

You can see the live samples and examples of C# DateTime format from the following links:

First of all create a string variable to store the string type DateTime.

string myDateTimeString;

Then create a new variable of type DateTime and pass the string to DateTime variable using Convert.ToDateTime function.

DateTime dt;
dt = Convert.ToDateTime(myDateTimeString);

Now you are ready to get the different parts of the Date String passed to the DateTime variable.

Response.Write(dt.Day + "/" + dt.Month + "/" + dt.Year);

Complete C# code to convert string to DateTime: 

string myDateTimeString;

myDateTimeString = "19 Feb,2008";

DateTime dt;dt = Convert.ToDateTime(myDateTimeString);

Response.Write(dt.Day + "/" + dt.Month + "/" + dt.Year);


Further you can format the DateTime using ToString("xxxxxx").


You can see the outputs of above C# code from the following links:

Convert String to DateTime

Date Formats using ToString Function

Time Formats using ToString Function

Learn DateTime ToString Formats here C# Date Format using ToString function.

Continue to next tutorial: ASP.Net 2.0 C# DateTime IFormatProvider Using ParseExact to learn culture based DateTime string conversion to DateTime object.

8 Responses to "C# Convert String to DateTime"
I am displaying calender on textbox click(using calender extention ajax).
Its in format "mm/dd/yyyy".
Am saving it in datetime datatype.
its giving error while converting textbox value to datetime format.
Thank you!!! This solved the following code: newCampagna.DataChiusura = Convert.ToDateTime(txtDataChiusura.Text); where txtDataChiusura is a textbox populated with a date picker. Error received: String was not recognized as a valid DateTime.
Can u tell me what error you got?
hi i am getting date from coded bits like 230710(ddmmyy) actually it is 23/07/10 i mean 23/07/2010.. getting error like "invalid string format to convert it to date" if i use convert.ToDateTime(s) where s='23-01-10'
plz suggest me wat can i do..???
Error Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method
thnx for your helpful examples
i need this so early

one more problem that i face in today is :

i want to convert a textbox value into date format dd-mm-yyyy . so u give me some solution of that problem.
@Nishant First you can convert the textbox value to DateTime object using Convert.ToDateTime(textbox1.Text) method. Next you can format the date value using ToString method. e.g.: Convert.ToDateTime(textbox1.Text).ToString("dd-MM-yyyy"); You can learn the date formatting patterns here: C# Function to Get Current DateTime Good luck
Thanks so much. it help me to finish project to shopping online ;) thanks
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email