1 For Each c As DataGridViewCell In DataGridView1.SelectedCells
2 A = DataGridView1(6, c.RowIndex).Value
3 B = DataGridView1(8, c.RowIndex).Value
4 Kekka = CStr(CDec(DataGridView1(6, c.RowIndex).Value) - CDec(DataGridView1(8, c.RowIndex).Value))
5 'If DataGridView1.SelectedCells(c.ColumnIndex).Value = Nothing Then
6 ' DataGridView1.SelectedCells(c.ColumnIndex).Value = ""
7 'End If
8 Next
1' hhmm → hh:mm
2Public Function EditTime(ByVal value As String) As String
3 Dim hh As String = Nothing
4 Dim mm As String = Nothing
5 DivideTime(value, hh, mm)
6 Return hh & ":" & mm
7End Function
89' hh:mm → hhmm
10Public Function RestoreTime(ByVal value As String) As String
11 Dim hh As String = Nothing
12 Dim mm As String = Nothing
13 DivideTime(value, hh, mm)
14 Return hh & mm
15End Function
1617' 入力文字列を TimeSpan に
18Public Function ToTimeSpan(ByVal value As String) As TimeSpan
19 Dim hh As String = Nothing
20 Dim mm As String = Nothing
21 DivideTime(value, hh, mm)
22 Return New TimeSpan(CInt(hh), CInt(mm), 0)
23End Function
2425' 左側にゼロを埋めて指定した桁数にする
26Private Function LPadZero(ByVal value As String, ByVal point As Integer) As String
27 Dim padStr As String = New String("0"c, point)
28 value = padStr & value
29 Return value.Substring(value.Length - point)
30End Function
3132' 入力文字列を hh, mm に分ける
33Private Sub DivideTime(ByVal value As String, ByRef hh As String, ByRef mm As String)
34 '「:」があるか検査
35 Dim index As Integer = value.IndexOf(":"c)
36 If index = -1 Then
37 ' 「:」が無いとき左側をゼロ埋めして4文字にする
38 value = LPadZero(value, 4)
39 ' 2文字に分ける
40 hh = value.Substring(0, 2)
41 mm = value.Substring(2, 2)
42 Else
43 '「:」がある場合は左右に分ける
44 hh = value.Substring(0, index)
45 mm = value.Substring(index + 1)
46 ' 左側をゼロ埋めして2文字にする
47 hh = LPadZero(hh, 2)
48 mm = LPadZero(mm, 2)
49 End If
50End Sub
使い方
VB
1Dim A As String = "09:00"
2Dim B As String = "17:45"
34Dim T As TimeSpan = ToTimeSpan(B) - ToTimeSpan(A)
5Dim TM As Double = T.TotalMinutes
6Dim h As Double = TM \ 60
7Dim m As Double = TM Mod 60
8Dim hhmm As String = h.ToString() & ":" & m.ToString()
9Dim result As String = EditTime(hhmm) ’ 08:45 になる
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/07/08 23:32