문제

I have string value 17:00:25

how do i display in Military time like 1700 only? im debugging it with like below

Dim time As String = String.Format("{0:hh:mm}", row("timereported"))
MessageBox.Show(time.ToString)

and the actual code is

For Each row As DataRow In table.Rows
     rowInsert = DS.Tables(0).NewRow
     Dim time As String = String.Format("{0:hh:mm}", row("timereported"))
     rowInsert(0) = time
     DS.Tables(0).Rows.Add(rowInsert)
Next row

but it display as 17:00:25

any other kinds of conversion or format suited to format?

도움이 되었습니까?

해결책

If the column is a TimeSpan...

For Each row As DataRow In table.Rows
  rowInsert = DS.Tables(0).NewRow
  Dim time As String = Format(New Date + CType(row("timereported"), TimeSpan), "HHmm") 'this line changed'
  rowInsert(0) = time
  DS.Tables(0).Rows.Add(rowInsert)
Next row

If the column is a string...

For Each row As DataRow In table.Rows
  rowInsert = DS.Tables(0).NewRow
  'new code starts'
  Dim time As String = ""
  Dim tsp As TimeSpan
  If TimeSpan.TryParse(CStr(row("timereported")), tsp) Then
    time = (New Date + tsp).ToString("HHmm")
  End If
  'new code ends'
  rowInsert(0) = time
  DS.Tables(0).Rows.Add(rowInsert)
Next row

다른 팁

Const militaryTimeFormat As String = "HHmm"

' convert to a string in military time
Dim input As String = DateTime.Now.ToString(militaryTimeFormat)
MsgBox(input)

I would suggest using the strongly typed Field method of the datarow to get the value as a TimeSpan (you mentioned that it is stored as a Time data type in mySQL so this is the correct .NET equivalent)

Then you can format the timespan. Note the Timespan formats are different from DateTime formats:

For Each row As DataRow In table.Rows
    rowInsert = DS.Tables(0).NewRow
    Dim time As String = row.Field(Of TimeSpan)("timereported").ToString("hhmm")
    rowInsert(0) = time
    DS.Tables(0).Rows.Add(rowInsert)
Next row

For reference this:

Dim ts As New TimeSpan(17, 0, 25)
Debug.WriteLine(ts.ToString("hhmm"))

Outputs

1700

I personally wouldn't attempt to format a string value to get a datetime value, otherwise you're denying yourself access to .net's inherent datetime handling features.

A safer way to handle the datetime value would be the following...

  • If returning data from your database, return as a datetime, or datetimeoffset value.
  • If it is a user entry and you are handling a string (which is the only scenario I can think of that would warrant this), then validate and cast to a datetime value.

The value should be of datetime before you attempt to format it.

This would leave you with a clean way to handle this using the format code "t" where you place your parameter, and no issues with invalid values.

Importantly it also makes your code culture safe if your site is multilingual. For example en-GB would display "17:00" while en-US would display "5:00pm".

So for your code it would then simply be...

Dim time As String = String.Format("{0:t}", row("timereported"))

This would give you the format "HH:mm" or for your example "17:00".

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top