-
May 17th, 2021, 03:08 PM
#1
Thread Starter
Lively Member
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.
-
May 17th, 2021, 03:58 PM
#2
Re: Convert from 24hr time in db to 12hr time in program
What is the datatype of your "time" DataColumn in the dt DataTable?
-
May 17th, 2021, 04:33 PM
#3
Thread Starter
Lively Member
Re: Convert from 24hr time in db to 12hr time in program
Originally Posted by dday9
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
-
May 17th, 2021, 04:33 PM
#4
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 17th, 2021, 04:42 PM
#5
Thread Starter
Lively Member
Re: Convert from 24hr time in db to 12hr time in program
Originally Posted by .paul.
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!
-
May 17th, 2021, 05:12 PM
#6
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
-
May 17th, 2021, 05:23 PM
#7
Thread Starter
Lively Member
Re: Convert from 24hr time in db to 12hr time in program
Originally Posted by Shaggy Hiker
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.
-
May 17th, 2021, 10:16 PM
#8
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
-
May 18th, 2021, 01:58 AM
#9
Re: Convert from 24hr time in db to 12hr time in program
Originally Posted by victorb17
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.
-
May 18th, 2021, 05:41 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|