In my ASP.NET (C#) project, I’m inserting some data into a database.
INSERT INTO USERS (username,join_date) VALUES('Ali',GETDATE());
Now when I’m fetching the date from the database, I’m using DateTime.
SqlDataReader r = Command.ExecuteReader();
r.Read();
myDate = r.getDateTime(0);
And when I’m inserting this in some DIV, I do this.
"<div>"+myDate.ToLongTimeString()+"</div>"
I get the correct date as in day/month/year, but the hour is always 12:00:00 AM.
How can I get the exact time?
Using
DATEas your column type means you are only storing the date not the time.Therefore when you read the value back into a
DateTimeobject the system has to initialise the time part – to midnight.Convert the column to
DATETIMEand you’ll get the time component stored as well.For more information on the difference see the MSDN page on Date and Time Data Types and Functions