Michigan Man
Michigan Man

Reputation: 101

SQL Server VB .NET (or C#) datetime Canonical

I am working with a canonical date in the format 2011-10-24 00:00:00.000.

There is a SQL Server statement that I can run to get the date in the format in that I need it in.

SELECT 
   CONVERT(VARCHAR(50), CONVERT(datetime, '2011-10-24 00:00:00.000', 120), 101)` as test

Returns

10/24/2011.

Here is my question.

Is there a way to do this, convert '2011-10-24 00:00:00.000' to 10/24/2011, in .Net (C# or VB) ?

Upvotes: 1

Views: 842

Answers (4)

Konrad Gadzina
Konrad Gadzina

Reputation: 3404

You can parse input string to DateTime object with DateTime.Parse and then format it as you wish with DateTime.ToString method.

Upvotes: 0

Gadget27
Gadget27

Reputation: 544

In VB

Dim dt As Date = Date.Parse("2011-10-24 00:00:00.000")
Dim newDateString as string = dt.ToShortDateString()

In C#

System.DateTime dt = System.DateTime.Parse("2011-10-24 00:00:00.000");
string newDateString = dt.ToShortDateString();

Upvotes: 0

Sam Axe
Sam Axe

Reputation: 33738

Why don't you just select it as a date instead of converting to VARCHAR(50) ?

If That's not an option then cast it to a date (DateTime.Parse) and use the properties on the datetime.

Upvotes: 1

Scott Selby
Scott Selby

Reputation: 9580

VB.Net

  Dim newDate As string = CDate(fromdb).ToShortDateString

c#

  string newDate = DateTime.Parse(fromdb).toShortDateString();

pull it from db as a DateTime , let c# / vb.net convert for you

Upvotes: 0

Related Questions