I want to branch according to the start time and end time values.
The original data is in the format of "HH: MM AM", "HH: MM PM", etc.

Details below.
・ I want to set AM12: 00 to 24:00 for work data across dates.
・ I want to set AM12: 00 to 00:00 for work data that does not cross dates.
・ Do not change the date.

Example 1 when straddling dates)
Start time PM08: 00 End time AM03: 00
Start time 2000 End time 2700

Example 2 When the date is not crossed)
Start time AM12: 00 End time AM08: 00
Start time 0000 End time 0800

Try it out

1. Get morning or afternoon

strStart = Microsoft.VisualBasic.Right (start time, 2)
strEnd = Microsoft.VisualBasic.Right (end time, 2)

2.When start time and end time are different between morning and afternoon
And start time"PM"

If Not strStart = strEnd And strStart = "PM" then

I don't really know how to put logic together.

If start time is"AM"and end time is"PM" I do n’t think it ’s crossing the sun.

Because the start time is"PM"at the branch
If the end time is"AM" ;, I think it should be converted to data exceeding 24:00.

Supplemental information (FW/tool version etc.)

Visual Studio 2015
.Framework 4.6


Work data created on other systems is
Saved as a CSV file.

The start time and end time fields of the CSV file are
The format is "HH: MM AM" and "HH: MM PM".

  • Answer # 1

    Set both original times to24-hour notation
    Can you do this? Whether00:00 AMis the end time or the start time, just set it to00:00. Then

    Add 24 hours to end time if end time is less than start time
    For example, if the start is09:00and the end is00:00, add 24 hours to the end time to make24:00. If the end is01:00, it will be25:00.

    Working time = End time-Start time

    If it is

    , the branch can be done only once in 2.

    .NET has a class or structure called TimeSpan that can easily calculate the time difference. please refer.

  • Answer # 2

    If no more than 24 hours have passed

    If start time>end time Then
        'Sun stride
    End If

    Is n’t it okay?

    Although I don't know how the data is retained,
    Even if it is only time to input at the time of input, I think that it is easier to add the date.

  • Answer # 3

    As YAmaGNZ says, it is easier to handle data by converting it to some data, such as a date, rather than a character string.

    Because it was troublesome to start VS, it is a scribble in PowerShell, but is it the following atmosphere?
    Since it does not use PowerShell-specific functions, please read VB appropriately.

    If you want to run as it is, paste it into Windows PowerShell ISE and click "Run".

    # input string
    [string] $inputStartTime = '08: 00 PM '
    [string] $inputEndTime = '03: 00 AM '
    # Convert to DateTime with System.DateTime class method
    # ParseExact may be better depending on the string format
    [datetime] $startDate = [datetime] :: Parse ($inputStartTime)
    [datetime] $endDate = [datetime] :: Parse ($inputEndTime)
    # Get pure time (Because there is no date information when converting to DateTime above, it is scary depending on execution timing)
    [timespan] $startSpan = $startDate.TimeOfDay
    [timespan] $endSpan = $endDate.TimeOfDay
    # -ge = Greater Equal =>=
    if ($startSpan -ge $endSpan) {
        # Start time is greater than end time → span date
        # Add one day
        $endSpan = $endSpan.Add ([timespan] :: FromDays (1))
    # Results console output
    [string] :: Format ('{0:00} {1:00}', $startSpan.Hours, $startSpan.Minutes)
    [string] :: Format ('{0:00} {1:00}', [math] :: Floor ($endSpan.TotalHours), $endSpan.Minutes)