-2

私の ASP.NET (C#) プロジェクトでは、いくつかのデータをデータベースに挿入しています。

INSERT INTO USERS (username,join_date) VALUES('Ali',GETDATE());

データベースから日付を取得するときは、DateTimeを使用しています。

SqlDataReader r = Command.ExecuteReader();
r.Read();
myDate = r.getDateTime(0);

そして、これをDIVに挿入するときは、これを行います。

"<div>"+myDate.ToLongTimeString()+"</div>"

日/月/年のように正しい日付を取得しますが、時間は常に12:00:00 AMです。

どうすれば正確な時刻を取得できますか?

4

2 に答える 2

7

join_date がデータベースの DATE ではなく DATETIME 型であることを確認してください

http://msdn.microsoft.com/en-us/library/ms186724.aspx

于 2013-01-12T22:46:19.303 に答える
4

Using DATE as your column type means you are only storing the date not the time.

Therefore when you read the value back into a DateTime object the system has to initialise the time part - to midnight.

Convert the column to DATETIME and 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

于 2013-01-12T22:49:14.263 に答える