SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Mad Beach Bum jgsketch's Avatar
    Join Date
    Aug 2002
    Location
    FL
    Posts
    825
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Converting DateTime to just Date not working

    I'm trying to convert my DateTime to just Date "dd-MM-yyyy". I cannot get it to work. It seems I'm missing something simple here. I've tried so many different ways of doing this, listed below. I'm now out of ideas.

    On my aspx.cs page
    Code:
    lblduedate.Text = dt.Rows[0]["DueDate"].ToString("dd-MM-yyyy");
    On my aspx page
    Code:
    <td>
    <asp:Label ID="lblduedate" runat="server" ></asp:Label>
    </td>
    I've also on the aspx page, tried different formats of
    Code:
    Text='<%# Eval("DueDate", "{0:dd/MM/yyyy}") %>'

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,629
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    What comes out?

    And what is the type of whatever is in your datatable?

  3. #3
    Mad Beach Bum jgsketch's Avatar
    Join Date
    Aug 2002
    Location
    FL
    Posts
    825
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Output is 3/10/2004 12:00:00 AM

    Data type is set to smalldatetime in the database.

    So I'm not sure how it's getting the longdatetime format.

  4. #4
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,629
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    It is a DateTime object, which can be output as any date format. You might have to cast it into a DateTime to use the ToString overloads like you are trying to use.

  5. #5
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    Yes, wwb is correct. The .ToString(format) you using is not doing anything. You first need to convert it to DateTime, then call you .ToString(format) on the DateTime object

    eg. lblduedate.Text = ((DateTime)dt.Rows[0]["DueDate"]).ToString("dd-MM-yyyy");


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •