How to set only Date in DataTable Column of Type DateTime WEB
First of all take only date part
dr[0] = DateTime.Now.Date;
Second make use of
<asp:Label ID="Label1" runat="server" Text='<%#
((DateTime)Eval("ItemValue")).ToShortDateString() %>'></asp:Label>
method to when binding value
EDIT
Not Sure but you can check this answer may help you to resolve your issue : Is it possible to format a date column of a datatable?
KhanZeeshan
Updated on June 02, 2020Comments
-
KhanZeeshan almost 4 years
Hi All,
My Question is simple but please keep in mind I'm not binding it to any grid or any of ASP.NET control I have my own grid control and I want to keep it as a DateTime Column for sorting purpose.
I'm creating DataTable With Column Type DateTime.
DataTable data = new DataTable(); data.Columns.Add("Invoice Date", typeof(DateTime)); DataRow dr = data.NewRow(); dr[0] = DateTime.Now; //Adding filled row to the DataTable object dataTable.Rows.Add(dr);
When the Value is shown on ASP.NET page it is show something like this:
"2/28/2011 12:00:00 AM"
I have 2 columns like this, In 1 column I want to show just date and in other column i want to show date as "Dec 2011", these formats can be achieved if i use DataColumn with type string but in that case sorting is not working properly.
Please help.
Thanks.
-
KhanZeeshan almost 13 yearsit doesn't matter it always put "12:00:00 AM" in data.
-
Pranay Rana almost 13 years@KhanZeeshan - try second option when binding use ToShortDateString function
-
KhanZeeshan almost 13 yearswhen i format the value the with my required format it gives the correct format but as soon as the row is added in DataTable "12:00:00 AM" is appended in the columns.
-
KhanZeeshan almost 13 yearsThanks Dude but i want my DataTable to hold Date only because I'm using that DataTable in few other places also & I don't want to put hacks just for this scenario.
-
VMAtm almost 13 yearsUse the .Date property while adding the
Row
-
KhanZeeshan almost 13 yearsI tried that but no luck, I even assigned just date to by using ToString("MM/dd/yyyy") but as soon as its value is set in row object "12:00:00 AM" is added to it
-
KhanZeeshan almost 13 yearsI think you didn't get my question or may be I didn't explain it correctly. I can format the date value retrieved from database but after doing some calculation on the record I add it in .NET's DataTable class object, before adding it in the DataTable object the value's format is correct as soon as the value is added in DataTable object time is added also that's what I don't want.
-
Domenic almost 13 yearsThe data table has no concept of "only date" or "only time". It stores nanoseconds since January 1, 0001 C.E.. It does not store dates, or times. Your software is displaying a string containing both date and time, because that is the default format for displaying nanoseconds since January 1, 0001 C.E.
-
Domenic almost 13 yearsTo make it clearer: if you say
dr[0] = DateTime.Today
, the data table says "Oh, someone gave me a .NET Date. How do I store those... ahah, I'll go figure out the number of nanoseconds since January 1, 0001 C.E. for that .NET date, and store it." This is not "incorrect"; rather, it's your confusion about UI layer versus storage format that is causing you to think it is. -
KhanZeeshan almost 13 yearsso it means if i want to show just date i should convert that Datacolumn to string type?
-
Domenic almost 13 yearsNo, since you want to show just date, you should do the showing logic in your UI layer. That's what it's there for.
-
Dai over 2 yearsI downvoted this answer because it disregards the fact that a "date" and a "datetime" value are distinct types (in a general sense). Types are, and should be, used to convey information about their data and I feel it's just wrong to represent a date value with a
System.DateTime
value. Consider that every major RDMBS and the ISO SQL standard al have distinctdate
anddatetime
column-types, so I feel it was a big mistake for .NET to haveDateTime
,TimeSpan
andDateTimeOffset
but no trueDate
type (until .NET 6 finally addedDateOnly
earlier this year).