我有一个关于时间跨度/持续时间管理的问题。
目前,我在数据库中有一个以mmss
格式表示时间的记录。例如: 5030,这意味着50分30秒。
我想在我的网页上以这样的格式显示:
我有办法做到这一点吗?数据库中存储的数据是字符串格式的。我目前正在ASP.NET应用程序中使用ASP.NET语言。
我搜索了所有的互联网,但我不断得到的结果代表时间,而不是一个持续时间。
目前,我是这样做的,但我仍然无法得到显示的时间:
If arrayValueInside.Length > 0 Then
If arrayValueInside(0) = "STOPPING TIME" Then
Dim strTime As String = arrayValueInside(1).Trim()
Select Case strTime.Length
Case 1
strTime = strTime & "s"
Case 2
strTime = strTime & "s"
Case 3
strTime = strTime.Substring(0, 1) & "m " & strTime.Substring(1, 2) & "s"
Case 4
strTime = strTime.Substring(0, 2) & "m " & strTime.Substring(2, 2) & "s"
' If Integer.Parse(strTime) >= 6000 Then
' Return strTime.Substring(0, 2) + "m" + strTime.Substring(1, 2) + "s"
' Else
' End If
Case 5
strTime = strTime.Substring(0, 3) & "m " & strTime.Substring(3, 2) & "s"
End Select
如果我提供的信息有什么不清楚的地方,请告诉我。
发布于 2018-08-15 21:26:44
由于时间格式是以mmss
形式表示的,我认为还应该考虑分钟值在某些情况下可能超过"99"
,可以用3个(或更多)数字表示。
TimeSpan结构有一个计算时间单位的内部机制,这在这里是有用的。所有的单位都在几天内被转换和测量。如果一个单位的值超过它的最大值,它将在下一个单位中重新计算。
因此,70分钟将变成1小时10分钟。
在这里,最右边的2个字符被认为代表秒值;其他所有字符(2个或更多)都表示分钟。
Dim input As String = "12845"
Dim seconds As Integer = Integer.Parse(input.Substring(input.Length - 2, 2))
Dim minutes As Integer = Integer.Parse(input.Substring(0, input.Length - 2))
Dim ts As New TimeSpan(0, 0, minutes, seconds)
Dim TimeFormat = $"{ts.Hours}h {ts.Minutes}m {ts.Seconds}s"
TimeFormat字符串将是2h 8m 45s
。
如果串内插不可用,请使用String.Format()方法:
Dim TimeFormat = String.Format("{0}h {1}m {2}s", ts.Hours, ts.Minutes, ts.Seconds)
一种稍加修改的方法,如果单位值为0,它不会返回单位度量。
如果输入字符串为"12045"
,则前面的方法将返回2h 0m 45s
。这个将返回2h 45s
。
Dim TimeFormat As String() = New String() {
(If(ts.Hours > 0, ts.Hours & "h", "")),
(If(ts.Minutes > 0, " " & ts.Minutes & "m", "")),
(If(ts.Seconds > 0, " " & ts.Seconds & "s", ""))
}
Dim output As String = $"{TimeFormat(0)}{TimeFormat(1)}{TimeFormat(2)}"
https://stackoverflow.com/questions/51868883
复制相似问题