首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >时间跨度或持续时间格式

时间跨度或持续时间格式
EN

Stack Overflow用户
提问于 2018-08-16 02:38:27
回答 1查看 81关注 0票数 0

我有一个关于时间跨度/持续时间管理的问题。

目前,我在数据库中有一个以mmss格式表示时间的记录。例如: 5030,这意味着50分30秒。

我想在我的网页上以这样的格式显示:

  • 例子: 4020到40米20。
  • 例子: 6012至1h 12s。
  • 例子: 6515至1h,4m15s。

我有办法做到这一点吗?数据库中存储的数据是字符串格式的。我目前正在ASP.NET应用程序中使用ASP.NET语言。

我搜索了所有的互联网,但我不断得到的结果代表时间,而不是一个持续时间。

目前,我是这样做的,但我仍然无法得到显示的时间:

代码语言:javascript
运行
复制
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

如果我提供的信息有什么不清楚的地方,请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-16 05:26:44

由于时间格式是以mmss形式表示的,我认为还应该考虑分钟值在某些情况下可能超过"99",可以用3个(或更多)数字表示。

TimeSpan结构有一个计算时间单位的内部机制,这在这里是有用的。所有的单位都在几天内被转换和测量。如果一个单位的值超过它的最大值,它将在下一个单位中重新计算。

因此,70分钟将变成1小时10分钟。

在这里,最右边的2个字符被认为代表秒值;其他所有字符(2个或更多)都表示分钟。

代码语言:javascript
运行
复制
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()方法:

代码语言:javascript
运行
复制
Dim TimeFormat = String.Format("{0}h {1}m {2}s", ts.Hours, ts.Minutes, ts.Seconds)

一种稍加修改的方法,如果单位值为0,它不会返回单位度量。

如果输入字符串为"12045",则前面的方法将返回2h 0m 45s。这个将返回2h 45s

代码语言:javascript
运行
复制
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)}"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51868883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档