我有 1 个关于时间跨度/持续时间管理的问题。
目前,我在数据库中有一条记录,以mmss
格式表示时间。示例: 5030 假设表示 50 分 30 秒。
我想以这种格式在我的网页上显示它:
有什么办法可以实现这一目标吗?存储在 DB 中的数据是字符串格式。我目前在 ASP.NET 应用程序中使用 VB.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
如果我提供的信息中有任何不清楚的地方,请告诉我。
由于时间格式以 形式呈现mmss
,我认为还应该考虑分钟值,在某些情况下,可能会超过"99"
并且可以用 3 个(或更多)数字表示。
的时间跨度结构具有内部机构,以计算时间,该时间可以是有用的这里的单位。所有单位都转置并以天为单位进行测量。如果一个单位的值超过其最大值,则将在下一个单位中重新计算。
因此,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)}"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句