format date time into dd-mm-yy

How do i format a date time to a string with the foloowing format
mm-dd-yy

01-12-04
LVL 2
DaFouAsked:
Who is Participating?
 
AlexFMCommented:
VB:

Dim d as DateTime
Dim s as string

d = DateTime.Now
s = d.ToString("MM-dd-yy"))
0
 
AlexFMCommented:
C#:

DateTime d = DateTime.Now;
string s = d.ToString("MM-dd-yy"));
0
 
DaFouAuthor Commented:
I have a SQLdatareader with a date in it. Somehoe the conversion fails when i try to use this syntax
SomeSqlDataReader("SomeDateField").ToString("dd-MM-yy")

lDR("fld_dt_TransmitDateOut").ToString("dd-MM-yy")      Run-time exception thrown : System.InvalidCastException - Conversion from string "dd-MM-yy" to type 'Integer' is not valid.      
0
 
AlexFMCommented:
DateTime structire doesn't hace ToString(Integer) method. This means that lDR("fld_dt_TransmitDateOut") field type is not DateTime in this case. You need to check what is it's type - maybe null.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.