GLP
GLP

Reputation: 3675

how to parse datetime to a certain format?

How to I format datetime from sql to certain format user defined in C#? For example, the datetime in sql is like 2011-02-06 12:00:21, the user defined format is mmddyyyy, I am expecting output is 02062011 (note that not 262011). How do I do the conversion in C#?

Upvotes: 0

Views: 116

Answers (3)

Icarus
Icarus

Reputation: 63966

This should do it:

((DateTime)reader["DateColumn"]).ToString("MMddyyyy");

Assuming reader["DateColumn"] returns a DateTime object

Upvotes: 1

Artur A
Artur A

Reputation: 9139

Be more specific. Consuming you have DateTime object. If you want to show it you can use ToString overload method:

new DateTime(2011, 2, 6).ToString("MMddyyyy");

Upvotes: 1

vlad
vlad

Reputation: 4778

all you need is the correct format in ToString. In your case, that's MMddyyyy. For example:

var myDate = new DateTime(2011,2,6,12,0,21);
Console.WriteLine(myDate.ToString("MMddyyyy"));

Upvotes: 1

Related Questions