Convert.ToDateTime Method in C#


The Convert.ToDateTime method converts a given value to a DateTime value.

The following is my string.

string myDateString;
myDateString = "09/09/2018";

Now, let us convert it using the Convert.ToDateTime() method.

DateTime result;
result = Convert.ToDateTime(myDateString);

Here is the complete example.

Example

 Live Demo

using System;
public class Demo {
   public static void Main() {
      string myDateString;
      DateTime result;
      myDateString = "09/09/2018";
      result = Convert.ToDateTime(myDateString);
      Console.WriteLine("'{0}' converted to {1}", myDateString, result);
   }
}

Output

'09/09/2018' converted to 9/9/2018 12:00:00 AM

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

654 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements