2014年7月20日 星期日

[VB.NET] 時間轉換(上下午時間轉為24小時制)

   Public Function ConvertTo24hr(ByVal time As String) As String

        Dim IntAM, IntPM As Integer
        Dim IntMStr, IntMStrDay, IntMStrMins, IntMStrYear, IntMStrMonth, IntMStrHours, IntMStrSec As String
        IntAM = InStr(time, "上午") '不等於0為有上午字串
        IntPM = InStr(time, "下午")
        If time <> "" Then
            If IntAM <> 0 Then
                time = time.Replace("上午", "")
                Dim time2 As String = time
                Dim timSplit As String() = time.Split("/")
                Dim timSplit2 As String() = time2.Split(" ")
                IntMStrYear = timSplit(0)
                IntMStrMonth = timSplit(1)
                IntMStrMonth = IntMStrMonth.PadLeft(2, "0")

                time = IntMStrYear + "-" + IntMStrMonth + "-" + timSplit(2)
                IntMStrDay = time.Substring(7, 3)
                IntMStrDay = IntMStrDay.Replace("-", "")
                IntMStrDay = IntMStrDay.Replace(" ", "")
                IntMStrDay = IntMStrDay.PadLeft(2, "0")
                time = IntMStrYear + "-" + IntMStrMonth + "-" + IntMStrDay + " " + timSplit2(2)
                Dim time3 As String() = time.Split(" ")
                Dim HrMiSe As String = time3(1)
                Dim HrMiSe2 As String() = HrMiSe.Split(":")
                IntMStrHours = HrMiSe2(0)
                IntMStrMins = HrMiSe2(1)
                IntMStrSec = HrMiSe2(2)
                IntMStr = Mid(time, 12, 2)
                IntAM = Convert.ToInt32(IntMStr)
                If IntAM = 12 Then
                    IntAM = IntAM - 12
                    IntMStr = IntAM.ToString
                    IntMStr = IntMStr.PadLeft(2, "0")
                Else
                    IntAM = 0 + IntAM
                    IntMStr = IntAM.ToString
                    IntMStr = IntMStr.PadLeft(2, "0")
                End If
                time = IntMStrYear + "-" + IntMStrMonth + "-" + IntMStrDay + " " + IntMStr + ":" + IntMStrMins + ":" + IntMStrSec
            ElseIf IntPM <> 0 Then
                time = time.Replace("下午", "")
                Dim time2 As String = time
                Dim timSplit As String() = time.Split("/")
                Dim timSplit2 As String() = time2.Split(" ")
                IntMStrYear = timSplit(0)
                IntMStrMonth = timSplit(1)
                IntMStrMonth = IntMStrMonth.PadLeft(2, "0")
                time = IntMStrYear + "-" + IntMStrMonth + "-" + timSplit(2)
                IntMStrDay = time.Substring(7, 3)
                IntMStrDay = IntMStrDay.Replace("-", "")
                IntMStrDay = IntMStrDay.Replace(" ", "")
                IntMStrDay = IntMStrDay.PadLeft(2, "0")
                time = IntMStrYear + "-" + IntMStrMonth + "-" + IntMStrDay + " " + timSplit2(2)
                Dim time3 As String() = time.Split(" ")
                Dim HrMiSe As String = time3(1)
                Dim HrMiSe2 As String() = HrMiSe.Split(":")
                IntMStrHours = HrMiSe2(0)
                IntMStrMins = HrMiSe2(1)
                IntMStrSec = HrMiSe2(2)
                IntMStr = Mid(time, 12, 2)
                IntAM = Convert.ToInt32(IntMStr)
                If IntAM = 12 Then
                    IntAM = IntAM
                    IntMStr = IntAM.ToString
                    IntMStr = IntMStr.PadLeft(2, "0")
                Else
                    IntAM = 12 + IntAM
                    IntMStr = IntAM.ToString
                    IntMStr = IntMStr.PadLeft(2, "0")
                End If
                time = IntMStrYear + "-" + IntMStrMonth + "-" + IntMStrDay + " " + IntMStr + ":" + IntMStrMins + ":" + IntMStrSec
            End If
        End If
       
        Return time
    End Function

沒有留言:

張貼留言