C# Convert String To DateTime

C# Convert String To DateTime

How to convert string into DateTime format is the question for beginners in C# or some intermediate developers. Here are several way’s to achieve that in .net.

using System;

namespace Convert.String.To.DateAndTime.Example
{
    class Program
    {
        static void Main(string[] args)
        {
          
          string dateString = "12-Dec-2012";
          
          DateTime dt;
          
          DateTime.TryParse(dateString, out dt);
          
          Console.WriteLine( dt);
          
          Console.ReadLine();         
              
        } 
    } 
}

Output 12/12/2012 12:00:00 AM

To handle null string’s or wrong date time format string’s, we are using DateTime.TryParse method.

Convert string to DateTime with Format provider Example

You may want to convert the given string into culture specific format. In below example I will convert the string with English en-GB format. The given date is 12-March-2012.

Expected output is 12/03/2012 as I will be using English UK culture.

using System;
using System.Globalization;

namespace Convert.String.To.DateAndTime.Example
{
    class Program
    {
        static void Main(string[] args)
        {
        
        CultureInfo formatprovider = new CultureInfo("en-GB");
        
          
          string dateString = "12-Mar-2012";
          
          DateTime dt;
          
          DateTime.TryParse(dateString, formatprovider, System.Globalization.DateTimeStyles.None, out dt);
          
          Console.WriteLine(dt);
          
          Console.ReadLine();         
              
        } 
    } 
}

Output 3/12/2012 12:00:00 AM

C# Convert String To DateTime yyyymmdd

Also we can see custom date Time format.

using System;
using System.Globalization;

namespace Convert.String.To.DateAndTime.Example
{
    class Program
    {
        static void Main(string[] args)
        {      
          
          string dateString = "12-Mar-2012";
          
          DateTime dt;
          
          DateTime.TryParse(dateString,out dt);
          
          Console.WriteLine("Convert To yyyy-MM-dd " + dt.ToString("yyyy-MM-dd"));
          Console.WriteLine("Convert To yyyy/MM/dd " + dt.ToString("yyyy/MM/dd"));
          Console.WriteLine("Convert To M " + dt.ToString("M"));
          Console.WriteLine("Convert To MM " + dt.ToString("MM"));
          Console.WriteLine("Convert To MMM " + dt.ToString("MMM"));
          
          Console.ReadLine();         
              
        } 
    } 
}

output

Convert To yyyy-MM-dd 2012-03-12
Convert To yyyy/MM/dd 2012/03/12
Convert To M March 12
Convert To MM 03
Convert To MMM Mar

Tags:

C#

C# Convert String To DateTimeAsp.net Convert String To ImageDateTimeTryParse using C# and VB.net exampleC# Parse method of DateTimeReplace string in C#C# Binary To StringConvert Array To Comma Separated String C#Linq Convert String Array To Int ArrayDateTime format Time StringC# DateTime Difference[C#] Remove Last Character From String

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.