help with datetime

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

    #1

    help with datetime

    In the database, we are storing time in 3 columns: hours, Minutes and
    AM,PM.
    After retrieving it from the database, I want to load these values into
    a DateTime variable. I haven't had much success.

    I would appreciate if anyone can help me in this matter

    thank you
    MB

  • Claes Bergefall

    #2
    Re: help with datetime

    1. Consider changing your database to store them using a real datetime
    datatype instead
    2. Consider changing your database to store them using a real datetime
    datatype instead
    3. Consider changing your database to store them using a real datetime
    datatype instead
    4. Ok, maybe you can't change your database :-) Try this:

    Dim hours As String = "07"
    Dim minutes As String = "23"
    Dim ampm As String = "am"

    Dim s As String = hours & ":" & minutes & " " & ampm
    Dim dt As DateTime = DateTime.TryPar se(s, "hh\:mm tt", Nothing)

    Note that the format string in this sample ("hh\:mm tt") assumes leading
    zeroes for single digit hours and minutes. If you don't have that you can
    use "h\:m tt", "h\:mm tt" or "hh\:m tt" instead (depending on what you
    have). See DateTimeFormatI nfo for more info on the format string.

    /claes


    "Mohan" <kasvis03@gmail .com> wrote in message
    news:1151501933 .445860.290860@ x69g2000cwx.goo glegroups.com.. .[color=blue]
    > In the database, we are storing time in 3 columns: hours, Minutes and
    > AM,PM.
    > After retrieving it from the database, I want to load these values into
    > a DateTime variable. I haven't had much success.
    >
    > I would appreciate if anyone can help me in this matter
    >
    > thank you
    > MB
    >[/color]


    Comment

    • Sanjib Biswas

      #3
      Re: help with datetime

      First of all storing hours, minutes, and AM/PM is not sufficient to
      correctly reproduce a DateTime object.

      public DateTime (int year,int month,int day,int hour,int minute,int
      second,int millisecond)

      Since in the database you are also storing AM/PM, so I assume you are
      storing hours in 12 hour format. So before calling DateTime constructor, set
      the hour field to 24 hours format.
      If (PM) { hour = (hr_from_DB < 12 ? (hr_from_DB + 12) : 0) ); } <- Note that
      the day field will still be wrong.

      DateTime dt = new DateTime(System .DateTime.Today .Year,
      System.DateTime .Today.Month, System.DateTime .Today.Day, hour, 50, 0, 0);

      "Mohan" <kasvis03@gmail .com> wrote in message
      news:1151501933 .445860.290860@ x69g2000cwx.goo glegroups.com.. .[color=blue]
      > In the database, we are storing time in 3 columns: hours, Minutes and
      > AM,PM.
      > After retrieving it from the database, I want to load these values into
      > a DateTime variable. I haven't had much success.
      >
      > I would appreciate if anyone can help me in this matter
      >
      > thank you
      > MB
      >[/color]


      Comment

      • Cor Ligthert [MVP]

        #4
        Re: help with datetime

        Mohan,

        1. Consider changing your database to store them using a real datetime
        datatype instead
        2. Consider changing your database to store them using a real datetime
        datatype instead
        3. Consider changing your database to store them using a real datetime
        datatype instead
        4. Ok, maybe you can't change your database :-) Than you can try this:

        \\\
        Dim dtm As New DateTime
        dtm = dtm.AddHours(CD bl("11"))
        dtm = dtm.AddMinutes( CDbl("50"))
        If "PM" = "PM" Then dtm = dtm.AddHours(12 )
        ///

        Althouhg I would use one of the first 3 advices.

        Cor

        "Mohan" <kasvis03@gmail .com> schreef in bericht
        news:1151501933 .445860.290860@ x69g2000cwx.goo glegroups.com.. .[color=blue]
        > In the database, we are storing time in 3 columns: hours, Minutes and
        > AM,PM.
        > After retrieving it from the database, I want to load these values into
        > a DateTime variable. I haven't had much success.
        >
        > I would appreciate if anyone can help me in this matter
        >
        > thank you
        > MB
        >[/color]


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: help with datetime

          "Mohan" <kasvis03@gmail .com> schrieb:[color=blue]
          > In the database, we are storing time in 3 columns: hours, Minutes and
          > AM,PM.
          > After retrieving it from the database, I want to load these values into
          > a DateTime variable. I haven't had much success.[/color]

          As others have already said, change the format you are storing the date
          values in. You may want to take a look at the 'SqlDateTime' type too.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          Working...