C# DateTime.Parse SQL Dates

SQL (Structured Query Language)

You have date strings from a database in string format and want to parse them into DateTime structs using the C# programming language. This is useful when you have database text-only data and your ADO.NET provider doesn't parse the dates automatically.

MySQL

You can use DateTime.Parse on the native date time format in the MySQL database. This is useful if for some reason you have output from the database in string format. Each DateTime.Parse call here succeeds, and returns the correct value.

Program that parses MySQL dates [C#]

using System;

class Program
{
    static void Main()
    {
	// Taken from MySQL: SELECT CURTIME()
	//                   SELECT TIME(...)
	string mySqlTime = "23:50:26";
	DateTime time = DateTime.Parse(mySqlTime);

	// Taken from MySQL: SELECT TIMESTAMP(...)
	string mySqlTimestamp = "2003-12-31 00:00:00";
	time = DateTime.Parse(mySqlTimestamp);
	Console.WriteLine(time);

	// Taken from MySQL: SELECT CURDATE()
	//                   SELECT DATE(...)
	string mySqlDate = "2008-06-13";
	time = DateTime.Parse(mySqlDate);
	Console.WriteLine(time);
    }
}

Output

12/31/2003 12:00:00 AM
6/13/2008 12:00:00 AM

Note on MySQL date formats. Normally MySQL dates and times are stored in columns of types DATETIME, DATE, and TIMESTAMP. These value types will be equivalent to the result of the above SELECTs. See section 11.6, Date and Time Functions. Link removed because the page is no longer available.

SQL Server

With the examples from Microsoft SQL Server, the DateTime.Parse method will accurately parse the string representations from the database. In SQL Server, dates are stored in the date, datetime2, and datetimeoffset types. See "Using Date and Time Data" at MSDN.

Program that uses DateTime.Parse with SQL Server [C#]

using System;

class Program
{
    static void Main()
    {
	// Taken from SQL SERVER: SELECT GETDATE()
	//                        SELECT GETUTCDATE()
	string sqlServerDate = "2007-04-30 13:10:02.047";
	DateTime time = DateTime.Parse(sqlServerDate);
	Console.WriteLine(time);

	// Taken from SQL SERVER: SELECT SYSDATETIME()
	//                        SELECT SYSUTCDATETIME()
	string sqlSysDate = "2007-04-30 20:10:02.0474381";
	time = DateTime.Parse(sqlSysDate);
	Console.WriteLine(time);

	// Taken from SQL SERVER: SELECT SYSDATETIMEOFFSET()
	string sqlSysDateOffset = "2007-04-30 13:10:02.0474381 -07:00";
	time = DateTime.Parse(sqlSysDateOffset);
	Console.WriteLine(time);
    }
}

Output

4/30/2007 1:10:02 PM
4/30/2007 8:10:02 PM
4/30/2007 1:10:02 PM

Format strings

Format illustration

When using the valid date formats from MySQL and SQL Server, you do not need to specify a format string or use ParseExact on the DateTime struct. In other words, you can simply use DateTime.Parse to get the dates from the SQL strings. In some cases this is not possible, and you will want to use a format string and ParseExact.

DateTime Format

Summary

.NET Framework information

We saw how you can parse MySQL and SQL Server 2005 date strings using the straightforward DateTime.Parse method. We checked the input and output against the MSDN and MySQL developer sites. You can use DateTime.Parse in its default overload to accomplish this task. This is needed when you are not using an ADO.NET provider that converts the dates for you.

Time Representations
.NET