Jeano Ermitaño
Jeano Ermitaño

Reputation: 179

DataTable Modifies Date Format - WHY?

So I issue a test query like SELECT * from my table with a start_date column that has a Date data type. This query is then made into a DataTable, after which it's converted to a CSV file. In SSMS, the query returns only the date, as you can see here:

SSMS Query

But when I downloaded the converted CSV file, the dates came out like this:

CSV file

What I want to know is why. Why does the DataTable behave like this? I can't find a direct answer in the documentation of DataTable.

This is on the ASP.NET 4.0 C# platform with SQL Server 2008.

Thanks!

Upvotes: 2

Views: 1711

Answers (2)

cyberj0g
cyberj0g

Reputation: 3787

DataTable uses .NET DateTime class and it probably get converted to string with DateTime.ToString() method when CSV is created. That method uses date format from current Thread's Culture settings. You need to use DateTime.ToString(string format) overload when creating CSV to specify desired format of DateTime.

Here's general description of culture-specific DateTime formatting on .NET platform.

Upvotes: 6

Soner Gönül
Soner Gönül

Reputation: 98740

I don't think this is a DataTable issue.

T-SQL date type mapped with DateTime CLR data type. That means, even this type contains only date part in SQL Server, when you map this to DateTime, it's time part will be midnight by default.

Other than that, this 2015-06-17 and 6/17/2015 12:00:00 AM values are just a different textual representation of this values. And textual representation depends on your current culture settings and/or CSV file settings etc..

Upvotes: 3

Related Questions