VB,将"秒"转成"时分秒"格式
参考博客:http://share.freesion.com/427239/
数据库存放字段worktime,(类型:varchar(6),说明:HHmmss),页面要求显示 HH:mm:ss
1.sql语句,可以将worktime的数据,转成 秒数
SELECT SUM( cast( substring( d13.`worktime`, 1, 2 ) AS DECIMAL ) * 3600 + cast( substring( d13.`worktime`, 3, 2 ) AS DECIMAL ) * 60+ cast( substring( d13.`worktime`, 5, 2 ) AS DECIMAL ) ) AS 'totalTime' FROM d13
2.vb后台,将 秒数 转成 时分秒的格式
label1.Text = Format(DateAdd("s", CDec(label1.Text), "00:00:00"), "HH:mm:ss")
CDec(表达式):是将表达式参数,转成Decimal类型的数据 totalTime
https://docs.microsoft.com/zh-cn/office/vba/Language/Reference/User-Interface-Help/dateadd-function DateAdd方法的介绍
DateAdd("s",秒数,"00:00:00"),将秒 转成 包含时分秒的日期
Format(object,"格式"),在这里,格式日期数据,返回string
公司同事用的方法,推荐一下
#Region "计算时间差返回HHMMSS" <WebMethod()> Public Function descHHmmss(ByVal _start As String, ByVal _end As String) As String Dim hhmmss As String = "" If _start.Trim <> "" AndAlso _end.Trim <> "" Then Dim _t1 As TimeSpan = TimeSpan.Parse(_start) Dim _t2 As TimeSpan = TimeSpan.Parse(_end) Dim _ts As TimeSpan = (_t2 - _t1) hhmmss = _ts.Hours.ToString.PadLeft(2, "0") & ":" & _ts.Minutes.ToString.PadLeft(2, "0") & ":" & _ts.Seconds.ToString.PadLeft(2, "0") End If Return hhmmss End Function #End Region
可以用TimeSpan,将字符串转成日期数据,从TimeSpan中,可以取到小时,分钟,秒数等等,如下图: