6

enter image description hereHi 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
  • 1,410
  • 5
  • 23
  • 36

2 Answers2

3

First of all take only date part

dr[0] = DateTime.Now.Date;

Second make use of

DateTime.ToShortDateString

    <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?

Community
  • 1
  • 1
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • 2
    it doesn't matter it always put "12:00:00 AM" in data. – KhanZeeshan Jun 02 '11 at 07:02
  • @KhanZeeshan - try second option when binding use ToShortDateString function – Pranay Rana Jun 02 '11 at 07:03
  • 1
    when 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 Jun 02 '11 at 07:03
  • Thanks 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. – KhanZeeshan Jun 02 '11 at 07:19
  • Use the .Date property while adding the `Row` – VMAtm Jun 02 '11 at 07:29
  • 1
    I 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 Jun 02 '11 at 07:35
0

You seem to be mixing your concepts. The purpose of the database, and the table inside it, is to store data---not to format it. It is an implementation detail exactly how this gets stored. Just like you wouldn't care whether the datatable uses UTF-8 or UTF-16 for storing your strings, you shouldn't care what format it stores a date in. In this case, it is almost certainly stored as the number of nanoseconds since 12:00 midnight, January 1, 0001 C.E. That is, if you actually dug around in the binary format of the database, you would find those nanoseconds.

Now, the purpose of your UI layer, i.e. the ASP.NET page, is to display the data. This is where you make any formatting decisions.

One UI layer is whatever software you're using to view the database. That software is converting some number of nanoseconds, which is the internal storage format, into the string "2/28/2011 12:00:00 AM". The reason it is doing this is because that is the default format for displaying points in time.

The UI layer you are coding---the one you have control over---now needs to choose how it converts those nanoseconds into text. If it goes with the default, it will display "2/28/2011 12:00:00 AM". But if you don't want the default, you need to actually specify that. Pranay Rana's answer gives an idea of how to do that. This is the whole point of your UI layer.

Domenic
  • 110,262
  • 41
  • 219
  • 271
  • I 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. – KhanZeeshan Jun 02 '11 at 07:46
  • The 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 Jun 02 '11 at 07:48
  • To 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. – Domenic Jun 02 '11 at 07:50
  • so it means if i want to show just date i should convert that Datacolumn to string type? – KhanZeeshan Jun 02 '11 at 07:50
  • No, since you want to **show** just date, you should do the **showing** logic in your UI layer. *That's what it's there for.* – Domenic Jun 02 '11 at 07:51
  • I 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 distinct `date` and `datetime` column-types, so I feel it was a big mistake for .NET to have `DateTime`, `TimeSpan` and `DateTimeOffset` but no true `Date` type (until .NET 6 finally added `DateOnly` earlier this year). – Dai Dec 16 '21 at 01:29