Returning a short date in a datareader

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • axapta

    Returning a short date in a datareader

    Hi,
    I have the following however the date is returned in the long format. I want
    to display as dd/mm/yyyy.

    If Not drReader.Item(" offerstatusdate ") Is DBNull.Value Then
    txtOfferStatusD ate.Text = drReader.Item(" offerstatusdate ")
    End If
    Regards

  • cfps.Christian

    #2
    Re: Returning a short date in a datareader

    On Jun 16, 9:58 am, "axapta" <jas.jac...@gma il.comwrote:
    Hi,
    I have the following however the date is returned in the long format. I want
    to display as dd/mm/yyyy.
    >
    If Not drReader.Item(" offerstatusdate ") Is DBNull.Value Then
    txtOfferStatusD ate.Text = drReader.Item(" offerstatusdate ")
    End If
    Regards
    if not drReader("offer statusdate") is dbnull.value then
    dim dtTempDate as datetime =
    Convert.toDateT ime(drReader("o fferstatusdate" )
    txtOfferStatusd ate.Text = dtTempdate.ToSh ortdateTimeStri ng()
    end if

    Comment

    • Jack Jackson

      #3
      Re: Returning a short date in a datareader

      On Mon, 16 Jun 2008 08:31:00 -0700 (PDT), "cfps.Christian "
      <ge0193387@otc. eduwrote:
      >On Jun 16, 9:58 am, "axapta" <jas.jac...@gma il.comwrote:
      >Hi,
      >I have the following however the date is returned in the long format. I want
      >to display as dd/mm/yyyy.
      >>
      > If Not drReader.Item(" offerstatusdate ") Is DBNull.Value Then
      > txtOfferStatusD ate.Text = drReader.Item(" offerstatusdate ")
      > End If
      >Regards
      >
      >if not drReader("offer statusdate") is dbnull.value then
      dim dtTempDate as datetime =
      >Convert.toDate Time(drReader(" offerstatusdate ")
      txtOfferStatusd ate.Text = dtTempdate.ToSh ortdateTimeStri ng()
      >end if

      Comment

      • Jack Jackson

        #4
        Re: Returning a short date in a datareader

        On Mon, 16 Jun 2008 08:31:00 -0700 (PDT), "cfps.Christian "
        <ge0193387@otc. eduwrote:
        >On Jun 16, 9:58 am, "axapta" <jas.jac...@gma il.comwrote:
        >Hi,
        >I have the following however the date is returned in the long format. I want
        >to display as dd/mm/yyyy.
        >>
        > If Not drReader.Item(" offerstatusdate ") Is DBNull.Value Then
        > txtOfferStatusD ate.Text = drReader.Item(" offerstatusdate ")
        > End If
        >Regards
        >
        >if not drReader("offer statusdate") is dbnull.value then
        dim dtTempDate as datetime =
        >Convert.toDate Time(drReader(" offerstatusdate ")
        txtOfferStatusd ate.Text = dtTempdate.ToSh ortdateTimeStri ng()
        >end if
        You could also use:

        txtOfferStatusd ate.Text =
        Convert.ToDateT ime(drReader("o fferstatusdate" )).ToShortDateS tring()

        or:

        Dim offerStatusDate Index As Integer = _
        drReader.GetOrd inal("offerstat usdate")

        If Not drReader.IsDBNu ll(offerStatusD ateIndex) Then
        txtOfferStatusd ate.Text = _
        drReader.GetDat eTime(offerStat usDateIndex).To ShortDateString ()

        Comment

        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

          #5
          Re: Returning a short date in a datareader

          axapta wrote:
          Hi,
          I have the following however the date is returned in the long format.
          That's most likely not a completely accurate description of what's
          happening.

          If the field is a datetime field in the database, it's not returned as a
          formatted string, it's returned as a DateTime value. That means that the
          actual formatting is done when you assign the value to the text property.
          I
          want to display as dd/mm/yyyy.
          >
          If Not drReader.Item(" offerstatusdate ") Is DBNull.Value Then
          txtOfferStatusD ate.Text = drReader.Item(" offerstatusdate ")
          End If
          Instead of reading the value as an Object and implicitly converting the
          DateTime value into a string, read the value as a DateTime value, and
          explicitly format it the way that you want it:

          txtOfferStatusD ate.Text =
          drReader.GetDat eTime(drReader. GetOrdinal("off erstatusdate")) .ToShortDateStr ing()

          or

          txtOfferStatusD ate.Text =
          drReader.GetDat eTime(drReader. GetOrdinal("off erstatusdate")) .ToString("d")

          or

          txtOfferStatusD ate.Text =
          drReader.GetDat eTime(drReader. GetOrdinal("off erstatusdate")) .ToString("dd/MM/yyyy")

          Note:
          If you are doing this in a loop, you can call GetOrdinal outside the
          loop and store the index of the field in an Integer variable.

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          • Cor Ligthert[MVP]

            #6
            Re: Returning a short date in a datareader

            >
            If the field is a datetime field in the database, it's not returned as a
            formatted string, it's returned as a DateTime value. That means that the
            actual formatting is done when you assign the value to the text property.
            >
            In my idea is that a not completely true, databases where returning kind of
            DateTime values long before there was the DotNet DateTime structure.
            Therefore the DateTime is represented as a value in an object, but not
            direct a DotNet DateTime Value.

            I never have investigated what value is returned, but surely not a DotNet
            DateTime, which is completely different from the ShortDate and DateTime used
            in databases even in its long format, at least before version SQL server
            2008.

            Cor

            Comment

            • =?ISO-8859-1?Q?G=F6ran_Andersson?=

              #7
              Re: Returning a short date in a datareader

              Cor Ligthert[MVP] wrote:
              >>
              >If the field is a datetime field in the database, it's not returned as
              >a formatted string, it's returned as a DateTime value. That means that
              >the actual formatting is done when you assign the value to the text
              >property.
              >>
              In my idea is that a not completely true, databases where returning kind
              of DateTime values long before there was the DotNet DateTime structure.
              Therefore the DateTime is represented as a value in an object, but not
              direct a DotNet DateTime Value.
              >
              I never have investigated what value is returned, but surely not a
              DotNet DateTime, which is completely different from the ShortDate and
              DateTime used in databases even in its long format, at least before
              version SQL server 2008.
              >
              Cor
              >
              Nitpicking, aren't we? ;)

              It's quite irrelevant what format the database uses to send the data to
              the database driver (and it probably varies depending on the database
              interface chosen). You can not access the data in that format, you can
              only access it as a DateTime value.

              --
              Göran Andersson
              _____
              Göran Anderssons privata hemsida.

              Comment

              • Cor Ligthert[MVP]

                #8
                Re: Returning a short date in a datareader


                "Göran Andersson" <guffa@guffa.co mschreef in bericht
                news:OfwI4hJ0IH A.2384@TK2MSFTN GP04.phx.gbl...
                Cor Ligthert[MVP] wrote:
                >>>
                >>If the field is a datetime field in the database, it's not returned as a
                >>formatted string, it's returned as a DateTime value. That means that the
                >>actual formatting is done when you assign the value to the text
                >>property.
                >>>
                >In my idea is that a not completely true, databases where returning kind
                >of DateTime values long before there was the DotNet DateTime structure.
                >Therefore the DateTime is represented as a value in an object, but not
                >direct a DotNet DateTime Value.
                >>
                >I never have investigated what value is returned, but surely not a DotNet
                >DateTime, which is completely different from the ShortDate and DateTime
                >used in databases even in its long format, at least before version SQL
                >server 2008.
                >>
                >Cor
                >>
                >
                Nitpicking, aren't we? ;)
                >
                It's quite irrelevant what format the database uses to send the data to
                the database driver (and it probably varies depending on the database
                interface chosen). You can not access the data in that format, you can
                only access it as a DateTime value.
                >
                Exactly

                :-)

                Cor

                Comment

                Working...