VB.net 中的转换日期是否具有精确解析?

Conversion Date in VB.net with exact parse?

我在将 DateTime 转换为 d/M/yyyy HH:mm:ss 时遇到问题。
很久才发现我的脚本问题是转换成date的问题?
你能帮我转换一下为什么它没有给出格式 require

    Function Calculating_billing_time(strStartDate As String, intDuration As Integer, Optional blnOVTDisabled As Boolean = False) As (intTSNormal As Integer, intTSOVT1 As Integer, intTSOVT1N As Integer, intTSOVT2 As Integer, intTSOVT2N As Integer, intTSOVT3 As Integer, intTSOVT3N As Integer)
        Dim intTSNormal As Integer = 0
        Dim intTSOVT1 As Integer = 0
        Dim intTSOVT1N As Integer = 0
        Dim intTSOVT2 As Integer = 0
        Dim intTSOVT2N As Integer = 0
        Dim intTSOVT3 As Integer = 0
        Dim intTSOVT3N As Integer = 0
        Dim strDateBorderAm As String
        Dim dtDateBorderAM As Date
        'Dim strDateBorder_6AM As String
        'Dim dtDateBorder_6AM As Date
        'Dim strDateDayAfterBorder_6AM As String
        'Dim dtDateDayAfterBorder_6AM As Date
        Dim strDateBorderPM As String
        Dim dtDateBorderPM As Date
        Dim strDateBorderNT As String
        Dim dtDateBorderNT As Date
        Dim strDateBorderMD As String
        Dim dtDateBorderMD As Date
        Dim strDateDayAfterBorderAM As String
        Dim dtDateDayAfterBorderAM As Date

        Dim dateformat2 As String

        Dim dateFormat As String
        ' transformation of the string to actual date system
        Dim dtStartDate As Date
        Dim dtEndDate As Date

        dateFormat = "M/d/yyyy HH:mm:ss"
        dateformat2 = "d/M/yyy HH:mm:ss"
        dtStartDate = Date.ParseExact(strStartDate, dateFormat, Globalization.CultureInfo.InvariantCulture)
        dtStartDate = dtStartDate.AddSeconds(-(dtStartDate.TimeOfDay.Seconds))
        dtEndDate = dtStartDate.AddSeconds(intDuration)

        Console.WriteLine("Date start: " & dtStartDate)
        Console.WriteLine("Date end : " & dtEndDate)

        If blnOVTDisabled = True Then
            intTSNormal = intDuration
        Else


            'strDateBorder_6AM = dtStartDate.Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 06:00:00"
            'dtDateBorder_6AM = Date.ParseExact(strDateBorder_6AM, dateFormat, Globalization.CultureInfo.InvariantCulture)

            strDateBorderAm = dtStartDate.Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 07:00:00"
            dtDateBorderAM = Date.ParseExact(strDateBorderAm, dateformat2, Globalization.CultureInfo.InvariantCulture)

            strDateBorderPM = dtStartDate.Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 18:00:00"
            dtDateBorderPM = Date.ParseExact(strDateBorderPM, dateformat2, Globalization.CultureInfo.InvariantCulture)

            strDateBorderNT = dtStartDate.Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 22:00:00"
            dtDateBorderNT = Date.ParseExact(strDateBorderNT, dateformat2, Globalization.CultureInfo.InvariantCulture)

            strDateBorderMD = dtStartDate.AddDays(1).Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 00:00:00"
            dtDateBorderMD = Date.ParseExact(strDateBorderMD, dateformat2, Globalization.CultureInfo.InvariantCulture)

            strDateDayAfterBorderAM = dtStartDate.AddDays(1).Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & "07:00:00"
            dtDateDayAfterBorderAM = Date.ParseExact(strDateBorderAm, dateformat2, Globalization.CultureInfo.InvariantCulture)

            'strDateDayAfterBorder_6AM = dtStartDate.AddDays(1).Day & "/" & dtStartDate.Month & "/" & dtStartDate.Year & " 06:00:00"
            'dtDateDayAfterBorder_6AM = Date.ParseExact(strDateBorderAm, dateFormat, Globalization.CultureInfo.InvariantCulture)

            Console.WriteLine("")
            'Console.WriteLine("Date Border AM: " & dtDateBorder_6AM)
            Console.WriteLine("Date Border AM: " & dtDateBorderAM)
            Console.WriteLine("Date Border PM:" & dtDateBorderPM)
            Console.WriteLine("Date Border Night:" & dtDateBorderNT)
            Console.WriteLine("Date Border Midnight:" & dtDateBorderMD)
            'Console.WriteLine("Date Day After Border 6 AM:" & dtDateDayAfterBorder_6AM)
            Console.WriteLine("Date Day After Border AM:" & dtDateDayAfterBorderAM)
            Console.WriteLine("")

            ' Start before 7h00 (AM)
            If (dtStartDate <= dtDateBorderAM) Then

                Console.WriteLine("Start before 7h00 (AM) for:" & dtStartDate.DayOfWeek)

                If dtStartDate.DayOfWeek.ToString = "Sunday" Then
                    intTSOVT2N += CInt(DateDiff(DateInterval.Second, dtStartDate, dtDateBorderAM).ToString)
                Else
                    intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtStartDate, dtDateBorderAM).ToString)
                End If

                Console.WriteLine("TSNormal " & intTSNormal)
                Console.WriteLine("TSOVT1 " & intTSOVT1)
                Console.WriteLine("TSOVT1N " & intTSOVT1N)
                Console.WriteLine("TSOVT2 " & intTSOVT2)
                Console.WriteLine("TSOVT2N " & intTSOVT2N)
                Console.WriteLine("TSOVT3 " & intTSOVT3)
                Console.WriteLine("TSOVT3N " & intTSOVT3N)
                Console.WriteLine("")
                Console.WriteLine("start date " & dtStartDate)
                Console.WriteLine("end date " & dtEndDate)
            End If

            'Start After 7h00 (AM)
            If dtStartDate >= dtDateBorderAM Then
                Console.WriteLine("Start After 7h00 (AM) for " & dtStartDate.DayOfWeek)

                If dtStartDate.DayOfWeek.ToString = "Saturday" Then
                    intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtStartDate, dtDateBorderPM).ToString)

                ElseIf dtStartDate.DayOfWeek.ToString = "Sunday" Then
                    intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtStartDate, dtDateBorderPM).ToString)
                Else
                    intTSNormal += CInt(DateDiff(DateInterval.Second, dtStartDate, dtDateBorderPM).ToString)

                    Console.WriteLine("TSNormal " & intTSNormal)
                    Console.WriteLine("TSOVT1 " & intTSOVT1)
                    Console.WriteLine("TSOVT1N " & intTSOVT1N)
                    Console.WriteLine("TSOVT2 " & intTSOVT2)
                    Console.WriteLine("TSOVT2N " & intTSOVT2N)
                    Console.WriteLine("TSOVT3 " & intTSOVT3)
                    Console.WriteLine("TSOVT3N " & intTSOVT3N)
                    Console.WriteLine("")
                    Console.WriteLine("start date " & dtStartDate)
                    Console.WriteLine("end date " & dtEndDate)

                End If

            End If

            ' Stop before 18h
            If dtEndDate <= dtDateBorderPM Then
                Console.WriteLine("Stop before 18h for" & dtEndDate.DayOfWeek)

                If dtEndDate.DayOfWeek.ToString = "Saturday" Then
                    intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtEndDate, dtDateBorderPM).ToString)

                ElseIf dtEndDate.DayOfWeek.ToString = "Sunday" Then
                    intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtEndDate, dtDateBorderPM).ToString)

                Else
                    intTSNormal += CInt(DateDiff(DateInterval.Second, dtEndDate, dtDateBorderPM))

                    Console.WriteLine("TSNormal " & intTSNormal)
                    Console.WriteLine("TSOVT1 " & intTSOVT1)
                    Console.WriteLine("TSOVT1N " & intTSOVT1N)
                    Console.WriteLine("TSOVT2 " & intTSOVT2)
                    Console.WriteLine("TSOVT2N " & intTSOVT2N)
                    Console.WriteLine("TSOVT3 " & intTSOVT3)
                    Console.WriteLine("TSOVT3N " & intTSOVT3N)
                    Console.WriteLine("")
                    Console.WriteLine("start date " & dtStartDate)
                    Console.WriteLine("end date " & dtEndDate)
                End If

            End If

            ' Stop After 18h and Before 22h
            If (dtEndDate > dtDateBorderPM) And (dtEndDate <= dtDateBorderNT) Then
                Console.WriteLine("Stop After 18h and Before 22h for " & dtEndDate.DayOfWeek)

                If (dtEndDate.DayOfWeek.ToString = "Saturday") Then

                    intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtEndDate).ToString)

                ElseIf dtEndDate.DayOfWeek.ToString = "Sunday" Then
                    intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtEndDate).ToString)

                Else
                    intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtEndDate).ToString)

                    Console.WriteLine("TSNormal " & intTSNormal)
                    Console.WriteLine("TSOVT1 " & intTSOVT1)
                    Console.WriteLine("TSOVT1N " & intTSOVT1N)
                    Console.WriteLine("TSOVT2 " & intTSOVT2)
                    Console.WriteLine("TSOVT2N " & intTSOVT2N)
                    Console.WriteLine("TSOVT3 " & intTSOVT3)
                    Console.WriteLine("TSOVT3N " & intTSOVT3N)
                    Console.WriteLine("")
                    Console.WriteLine("start date " & dtStartDate)
                    Console.WriteLine("end date " & dtEndDate)

                End If

            End If

            'Stop After 22h and Before 07h00 (AM) Day After
            If dtEndDate >= dtDateBorderNT And dtEndDate <= dtDateDayAfterBorderAM Then

                Console.WriteLine("Stop After 22h and Before 07h00 (AM) day After for" & (dtEndDate.DayOfWeek))

                If dtEndDate.DayOfWeek.ToString = "Saturday" Then

                    intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtDateBorderNT).ToString)
                    intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtEndDate).ToString)

                ElseIf dtEndDate.DayOfWeek.ToString = "Sunday" Then

                    intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtDateBorderNT).ToString)
                    intTSOVT2N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtEndDate).ToString)

                Else
                    intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtDateBorderPM).ToString)
                    intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtEndDate, dtDateBorderNT).ToString)

                End If

                Console.WriteLine("TSNormal " & intTSNormal)
                Console.WriteLine("TSOVT1 " & intTSOVT1)
                Console.WriteLine("TSOVT1N " & intTSOVT1N)
                Console.WriteLine("TSOVT2 " & intTSOVT2)
                Console.WriteLine("TSOVT2N " & intTSOVT2N)
                Console.WriteLine("TSOVT3 " & intTSOVT3)
                Console.WriteLine("TSOVT3N " & intTSOVT3N)
                Console.WriteLine("")
                Console.WriteLine("start date " & dtStartDate)
                Console.WriteLine("end date " & dtEndDate)
            End If


            'Stop After 07h00 (AM) Day After
            If dtEndDate > dtDateBorderNT And dtEndDate > dtDateDayAfterBorderAM Then

                Console.WriteLine("Stop After 07h00 (AM) Day After for " & dtEndDate.DayOfWeek)

                If dtEndDate.DayOfWeek.ToString = "Saturday" Then

                    intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtDateDayAfterBorderAM).ToString)
                    intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateDayAfterBorderAM, dtEndDate).ToString)

                ElseIf dtEndDate.DayOfWeek.ToString = "Sunday" Then

                    intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtDateBorderMD).ToString)
                    intTSOVT2N += CInt(DateDiff(DateInterval.Second, dtDateBorderMD, dtDateDayAfterBorderAM).ToString)
                    intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtDateDayAfterBorderAM, dtEndDate).ToString)

                ElseIf dtEndDate.DayOfWeek.ToString = "Monday" Then

                    intTSOVT2 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtDateBorderNT).ToString)
                    intTSOVT2N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtDateBorderMD).ToString)
                    intTSOVT2N += CInt(DateDiff(DateInterval.Second, dtDateBorderMD, dtDateDayAfterBorderAM).ToString)
                    intTSNormal += CInt(DateDiff(DateInterval.Second, dtDateDayAfterBorderAM, dtEndDate).ToString)

                Else
                    intTSOVT1 += CInt(DateDiff(DateInterval.Second, dtDateBorderPM, dtDateBorderNT).ToString)
                    intTSOVT1N += CInt(DateDiff(DateInterval.Second, dtDateBorderNT, dtDateDayAfterBorderAM).ToString)
                    intTSNormal += CInt(DateDiff(DateInterval.Second, dtDateDayAfterBorderAM, dtEndDate).ToString)

                End If
                Console.WriteLine("")
                Console.WriteLine("TSNormal:" & intTSNormal)
                Console.WriteLine("intTSOVT1:" & intTSOVT1)
                Console.WriteLine("intTSOVT1N:" & intTSOVT1N)
                Console.WriteLine("TSOVT2:" & intTSOVT2)
                Console.WriteLine("TSOVT2N:" & intTSOVT2N)
                Console.WriteLine("intTSOVT3N:" & intTSOVT3N)
                Console.WriteLine("")



            End If



            Return (intTSNormal, intTSOVT1, intTSOVT1N, intTSOVT2, intTSOVT2N, intTSOVT3, intTSOVT3N)

        End If 'le generale 
    End Function

End Module

你相信,它会影响脚本的其余部分吗?
能告诉我怎么转换吗?

处理日期的更好方法

    Dim strDateBorderAm As DateTime = dtStartDate.Date.AddHours(7)
    Dim strDateBorderPM As DateTime = dtStartDate.Date.AddHours(18)
    ' etc

我没有收到任何错误。

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim strStartDate = "9/7/2021 07:00:00"
    Dim DateFormat = "M/d/yyyy HH:mm:ss"
    Dim dtStartDate = Date.ParseExact(strStartDate, DateFormat, Globalization.CultureInfo.InvariantCulture)
    Debug.Print(dtStartDate.ToString) 'Immediate window shows 9/7/2021 7:00:00 AM
End Sub