C# Convert String to DateTime

Updated on 09 Jun 2009, 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);

Output: 19/2/2008 

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

Output:

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.

Product Spotlight

Share it
Rate this article:
Email it:
email

0 Responses to "C# Convert String to DateTime"
Leave a Reply

Name   (Required)


Mail   (will not be published) (Required)


Website   (http://www.example.com)




OR Subscribe via Email: