Conditionally format Date/Time Values based on Time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?ZGNoMw==?=

    #1

    Conditionally format Date/Time Values based on Time

    Is there a way to conditionally format a dateTime field to produce on result
    if the value is 10/31/2008 12:00 AM (user didn't enter a time) and another
    result if the value is 10/31/2008 5:30 PM (user entered a time). I'm looking
    for this result...

    Value Result
    10/31/2008 12:00 AM FRI 10/31/08
    10/31/2008 12:01 AM FRI 10/31/08 12:01 AM

    I also need to figure out the cell spacing to force the time onto a
    different line as in

    10/31/2008 12:01 AM FRI 10/31/08
    12:01 AM

    David H
  • Mark Rae [MVP]

    #2
    Re: Conditionally format Date/Time Values based on Time

    "dch3" <dch3@discussio ns.microsoft.co mwrote in message
    news:A52CB169-710C-4321-92B0-6FB7AD953F39@mi crosoft.com...
    Is there a way to conditionally format a dateTime field
    A dateTime field...?

    The formats you are looking for are simple enough to achieve in code - how
    exactly that is done in practice will depend what you mean by a dateTime
    field. Are you talking about a column in a GridView, a TextBox, a Label...?


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • Nathan Sokalski

      #3
      Re: Conditionally format Date/Time Values based on Time

      I'm guessing you would probably do it the same way as any other conditional
      statement, where the conditional determines the format string to use. What
      you will use as the condition may be a little tricky, since it is obviously
      quite possible for the user to enter a time of 12:00 AM, so looking at the
      time entered would not be enough to determine whether a time was entered.
      How you will apply the format string will depend on where you are displaying
      the date and possibly a time. If it is simply a TextBox or Label or some
      other control with just a simple Text property, you can just use the
      DateTime.ToStri ng(formatstring ) method. If you are using a databound control
      such as a Repeater or DataList, you may need to do it in the ItemDataBound
      event. I'm not going to get into details of what you should do, since I
      haven't seen your code, but hopefully this can get you started. Good Luck!
      --
      Nathan Sokalski
      njsokalski@hotm ail.com
      有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


      "dch3" <dch3@discussio ns.microsoft.co mwrote in message
      news:A52CB169-710C-4321-92B0-6FB7AD953F39@mi crosoft.com...
      Is there a way to conditionally format a dateTime field to produce on
      result
      if the value is 10/31/2008 12:00 AM (user didn't enter a time) and another
      result if the value is 10/31/2008 5:30 PM (user entered a time). I'm
      looking
      for this result...
      >
      Value Result
      10/31/2008 12:00 AM FRI 10/31/08
      10/31/2008 12:01 AM FRI 10/31/08 12:01 AM
      >
      I also need to figure out the cell spacing to force the time onto a
      different line as in
      >
      10/31/2008 12:01 AM FRI 10/31/08
      12:01 AM
      >
      David H

      Comment

      • =?Utf-8?B?ZGNoMw==?=

        #4
        Re: Conditionally format Date/Time Values based on Time

        A field from a database displayed in a GridView. My experience is with
        Classic ASP. I just started playing with ASP.NET tonight. (Damn I wish I had
        tried this sooner)

        "Mark Rae [MVP]" wrote:
        "dch3" <dch3@discussio ns.microsoft.co mwrote in message
        news:A52CB169-710C-4321-92B0-6FB7AD953F39@mi crosoft.com...
        >
        Is there a way to conditionally format a dateTime field
        >
        A dateTime field...?
        >
        The formats you are looking for are simple enough to achieve in code - how
        exactly that is done in practice will depend what you mean by a dateTime
        field. Are you talking about a column in a GridView, a TextBox, a Label...?
        >
        >
        --
        Mark Rae
        ASP.NET MVP

        >
        >

        Comment

        • Mark Rae [MVP]

          #5
          Re: Conditionally format Date/Time Values based on Time

          "dch3" <dch3@discussio ns.microsoft.co mwrote in message
          news:491BF549-C16C-44DB-BDD1-58604F732B71@mi crosoft.com...
          >>Is there a way to conditionally format a dateTime field
          >>
          >A dateTime field...?
          >
          A field from a database displayed in a GridView.
          Ah...
          My experience is with Classic ASP.
          The best advice I can give you is to forget everything you ever knew about
          ASP Classic and learn ASP.NET properly from the ground up. The biggest
          mistake people make when coming from ASP Classic to ASP.NET is to assume
          that ASP.NET is somehow the "next version" of ASP Classic. It isn't - not
          even close. ASP.NET is different from ASP Classic in almost every way,
          mainly due to the .NET Framework.

          It's a steep learning curve. I'd suggest you get a copy of these:
          http://www.amazon.com/Beginning-ASP-...6236712&sr=8-2
          http://www.amazon.com/Pro-ASP-NET-20...6236712&sr=8-1
          and work your way through them.

          Anyway, to answer your question, you'll need to wire up the GridView's
          OnRowDataBound event:


          This will allow you to modify the values of a row's cells as they are being
          bound to the GridView. E.g. if the date value was contained in the third
          column of the GridView, you might do something like this:

          <asp:GridView ID="gvResults" runat="server"
          OnRowDataBound= "gvResults_RowD ataBound">

          protected void gvResults_RowDa taBound(object sender, GridViewRowEven tArgs e)
          {
          if (e.Row.RowType == DataControlRowT ype.DataRow)
          {
          DateTime dtmCell = DateTime.Parse( e.Row.Cells[2].Text);
          e,Row.Cells[2].Text = dtmCell.ToStrin g("ddd MM/dd/yy").ToUpper( );
          if (dtmCell.Hour + dtmCell.Minute + & dtmCell.Second 0)
          {
          e.Row.Cells[2].Text += "<br />" + dtm.Cell.ToStri ng("hh:mm tt");
          }
          }
          }

          Also, be aware that the date format FRI 10/31/08 is not Y2K-compliant
          because it doesn't display a 4-digit year, and is also internationally
          ambiguous. E.g. in that format, today's date would be

          "SUN 11/09/08"

          In the vast majority of the world, that would be interpreted as 11th
          September 2008, not 9th November 2008 and, depending on what it was used
          for, would fail validation because 11th September 2008 was a Tuesday, not a
          Sunday. In some parts of the world, that would be interpreted as 8th
          September 2011.

          If your web app is intended to be used by anyone outside the US and some
          parts of Canada, Kenya and the Philippines, you should not use this date
          format:



          --
          Mark Rae
          ASP.NET MVP


          Comment

          Working...