Date Formats of Date in Database

M

Martin Eyles

Hi,
I am trying to extract a date from a database, and display it in
yyyy-mm-dd hh:mm:ss format on an ASP.NET webpage. I have been using the CStr
function to turn this into text, but this doesn't give me enough control to
get the right date format. Can anyone suggest a solution?

Thanks,
Martin


'Snippet of Code I Tried
SQL = ("SELECT " & aDateTime)
anSQLCommand = New System.Data.SqlClient.SqlCommand
anSQLCommand.Connection() = conn
anSQLCommand.CommandText = SQL
R = anSQLCommand.ExecuteReader
While R.Read
displayADateTime = CStr(R(0))
End While
 
M

Martin Eyles

Maarten said:
R(0).ToString("yyyy-mm-dd")

This gives a compiler error - path\filename.ascx.vb(line): 'Public
Overridable Function ToString() As String' has no parameters and its return
type cannot be indexed.

Any more ideas?

Thanks,
Martin
 
P

Pipo

Sure...

Dim strDate as string = R(0).ToString()
strDate = strDate.ToString("mm-yyyy-dd")
 
P

Pipo

But that's also not the good one ;->

Here is 1:
DateTime dte;
if (DateTime.TryParse(R[0].ToString(), out dte))
{
string strDate = dte.ToString("MM-yyyy-dd");
}
 
M

Martin Eyles

Martin Eyles said:
This gives a compiler error - path\filename.ascx.vb(line): 'Public
Overridable Function ToString() As String' has no parameters and its
return type cannot be indexed.

Any more ideas?
It's OK, have fixed the above suggestion, by converting to date before
converting to string - I think it is awkward because the value comes from
the database. The code I use is:-
CDate(R(0)).ToString("yyyy-MM-dd HH:mm:ss")


Thanks again to everyone for their help,

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top