Results 1 to 10 of 10

Thread: Convert from 24hr time in db to 12hr time in program

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    117

    Convert from 24hr time in db to 12hr time in program

    I have tried many solutions I found on google, and non work. I have a db with a time(0) column. It lists times like this: 16:01:00. I would like the program to show it as: 4:01 PM. I can't seem to get it to work.

    The most recent thing I tried is this:

    Format("h:mm AM/PM", dt.Rows(rowcount)("time").ToString) This is being displayed in a 'detailed' listview column if it matters.
    Any help is appreciated.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Convert from 24hr time in db to 12hr time in program

    What is the datatype of your "time" DataColumn in the dt DataTable?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    117

    Re: Convert from 24hr time in db to 12hr time in program

    Quote Originally Posted by dday9 View Post
    What is the datatype of your "time" DataColumn in the dt DataTable?
    I'm not declaring it. Here is how the dt is created:

    Dim dt As New DataTable

    Then I use da.Fill(dt) where da is a dataAdapter

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Convert from 24hr time in db to 12hr time in program

    Try this...

    Code:
    Dim dt As TimeSpan = TimeSpan.Parse("16:01:00")
    Dim dateTime = New DateTime(dt.Ticks)
    Dim formattedTime = dateTime.ToString("h:mm:ss tt", CultureInfo.InvariantCulture)
    MsgBox(formattedTime)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    117

    Re: Convert from 24hr time in db to 12hr time in program

    Quote Originally Posted by .paul. View Post
    Try this...

    Code:
    Dim dt As TimeSpan = TimeSpan.Parse("16:01:00")
    Dim dateTime = New DateTime(dt.Ticks)
    Dim formattedTime = dateTime.ToString("h:mm:ss tt", CultureInfo.InvariantCulture)
    MsgBox(formattedTime)
    This worked, Thanks!

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Convert from 24hr time in db to 12hr time in program

    My quibble with that is the use of a variable named dateTime. A name that isn't also a data type would reduce confusion.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    117

    Re: Convert from 24hr time in db to 12hr time in program

    Quote Originally Posted by Shaggy Hiker View Post
    My quibble with that is the use of a variable named dateTime. A name that isn't also a data type would reduce confusion.
    I had already changed it.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Convert from 24hr time in db to 12hr time in program

    @.paul. - It is worth noting that the conversion from TimeSpan to DateTime is not needed. You can use DateTime's TryParseExact:
    Code:
    Dim dt = DateTime.ParseExact("16:01:00", "HH:mm:ss", Globalization.CultureInfo.InvariantCulture)
    Dim formattedTime = dt.ToString("h:mm tt", Globalization.CultureInfo.InvariantCulture)
    Fiddle: https://dotnetfiddle.net/N8QMZ1

    @victorb17 - Not knowing the data type is no excuse for not figuring it out. If you would have setup a breakpoint on the line where you populate the DataTable, hit F10 to step forward, and then added the following to your watch window then you would have been able to answer my question:
    Code:
    dt.Columns("time").DataType
    The reason I asked the question is because if the field was defined as a TimeSpan then you could have used the following:
    Code:
    Dim time = table.Rows(0).Field(Of TimeSpan)("time")
    Dim dt = DateTime.ParseExact(time.ToString(), "HH:mm:ss", Globalization.CultureInfo.InvariantCulture)
    Dim formattedTime = dt.ToString("h:mm tt", CultureInfo.InvariantCulture)
    Fiddle: https://dotnetfiddle.net/oiRuP9
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Convert from 24hr time in db to 12hr time in program

    Quote Originally Posted by victorb17 View Post
    I'm not declaring it. Here is how the dt is created:

    Dim dt As New DataTable

    Then I use da.Fill(dt) where da is a dataAdapter
    if you are using a DataAdapter then the field should be a Time or DateTime in the Database, be careful not to make
    a mess of your Table in the Database (don't know if you update that value ???)

    you can load from the Databse with the original value(24Hour) and display(12Hour)
    Code:
    Imports System.Globalization
    
    Public Class Form1
        Dim tb As New DataTable
        
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            tb.Columns.Add("ID")
            tb.Columns.Add("Time24Format")
            tb.Columns.Add("Time12Format")
            tb.Rows.Add("1", "16:01:00", "")
            tb.Rows.Add("2", "06:00:00", "")
            tb.Rows.Add("3", "22:02:00", "")
            tb.Rows.Add("4", "06:15:00", "")
            tb.Rows.Add("5", "17:10:00", "")
            DataGridView1.DataSource = tb
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'add the 12Hour
            Dim cultureSource = New CultureInfo("de-DE", False)
            Dim cultureDest = New CultureInfo("en-US", False)
            For i As Integer = 0 To DataGridView1.Rows.Count - 2
                Dim source = DataGridView1.Rows(i).Cells(1).Value
                Dim dt = DateTime.Parse(source, cultureSource)
                DataGridView1.Rows(i).Cells(2).Value = dt.ToString("t", cultureDest)
            Next
        End Sub
    End Class
    Last edited by ChrisE; May 18th, 2021 at 02:02 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  10. #10
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Convert from 24hr time in db to 12hr time in program

    Glancing at the code posted in this thread I noticed people are using Option Infer turned on. Personally I prefer it turned on. Also why not use the Nothing keyword instead of an empty pair of quotes?

Posting Permissions

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



Click Here to Expand Forum to Full Width