How to set only Date in DataTable Column of Type DateTime WEB

29,394

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?

Share:
29,394
KhanZeeshan
Author by

KhanZeeshan

Updated on June 02, 2020

Comments

  • KhanZeeshan
    KhanZeeshan almost 4 years

    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
    KhanZeeshan almost 13 years
    it doesn't matter it always put "12:00:00 AM" in data.
  • Pranay Rana
    Pranay Rana almost 13 years
    @KhanZeeshan - try second option when binding use ToShortDateString function
  • KhanZeeshan
    KhanZeeshan almost 13 years
    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
    KhanZeeshan almost 13 years
    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.
  • VMAtm
    VMAtm almost 13 years
    Use the .Date property while adding the Row
  • KhanZeeshan
    KhanZeeshan almost 13 years
    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
    KhanZeeshan almost 13 years
    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.
  • Domenic
    Domenic almost 13 years
    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
    Domenic almost 13 years
    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.
  • KhanZeeshan
    KhanZeeshan almost 13 years
    so it means if i want to show just date i should convert that Datacolumn to string type?
  • Domenic
    Domenic almost 13 years
    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.
  • Dai
    Dai over 2 years
    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).