Date Formats of Date in Database

  • Thread starter Thread starter Martin Eyles
  • Start date Start date
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
 
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
 
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");
}
 
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
 
Back
Top