使用vb.net在给定时间内找到数组中最近的时间

时间:2014-02-14 07:42:43

标签: mysql database vb.net time arraylist

我无法弄清楚如何在Vb.net中做到这一点,因为我是新手。我也不知道是否已经存在这种语法,或者可能没有正确的逻辑。 情况如下:

我有一张会员日志表,每位会员每天可以登录或退出几次。 其中一些是白班,一些是夜班。我已经获得了当天最小登录时间和当天或第二天(如果是墓地)的最大logoutTime的正确结果。但是因为数据就是这样的问题:

                          Table_Logs    

MembersName    LoginTime     LoginDate      LogoutTime    LogoutDate

Banog, Anna     00:26:00     2014-02-11     01:00:00       2014-02-11
Banog, Anna     12:49:00     2014-02-11     12:49:00       2014-02-11
Banog, Anna     13:16:00     2014-02-11     01:01:00       2014-02-12


                          Table_Schedule

MembersName    ScheduleDate      ScheduleTime

Banog, Anna     2014-02-11         13:30:00

Result: 
MinimumLoginTime = 00:26:00
MaximumLogoutTime = 01:01:00


Desired Result:
MinimumLoginTime = 12:49:00
MaximumLogoutTime = 01:01:00

我不知道为什么时间“00:26:00”在那里,但由于它是来自数据库的数据,所以必须解决它。这里唯一明确的是我需要将table_logs中的时间(数组)与table_schedule进行比较,并找到最接近loginTime的scheduleTime。到目前为止,这是我的代码:

 Dim mylist2 As New List(Of List(Of String))
 Dim mylist1 As New List(Of List(Of String))

 For i As Integer = 0 To x - 1

 query = "Select Count(*), agentName, loginDate, Min(loginTime) as loginTime, Max(logoutDate) as logoutDate FROM tbl_login WHERE agentName='" & mylist(i)(1).ToString & "' AND loginDate ='" & DateTimePicker1.Text & "'"
            cmd = New MySqlCommand(query, SQLConnection)
            count = cmd.ExecuteScalar()
            If count >= 1 Then
                Using reader As MySqlDataReader = cmd.ExecuteReader()
                    While reader.Read()
                        Dim ls As New List(Of String)
                        agentName = reader.GetString(1)
                        Dim LoginDates As Date = reader.GetString(2)
                        loginDate = LoginDates.ToString("yyyy-MM-dd")
                        Dim timeIn As Date = reader.GetString(3)
                        actualLoginTime = timeIn.ToString("HH:mm:ss")
                        Dim logoutDate As Date = reader.GetString(4)
                        actualLogoutDate = logoutDate.ToString("yyyy-MM-dd")
                        ls.Add(agentName)
                        ls.Add(loginDate)
                        ls.Add(actualLoginTime)
                        ls.Add(actualLogoutDate)
                        mylist1.Add(ls)
                    End While
                    reader.Close()
                End Using
            Else
                Dim ls As New List(Of String)
                agentName = "Absent"
                loginDate = "Absent"
                ls.Add(agentName)
                ls.Add(loginDate)
                ls.Add("00:00:00")
                ls.Add("Absent")
                mylist1.Add(ls)
            End If

            If mylist(i)(1).ToString() = "Absent " Then
                Dim ls As New List(Of String)
                ls.Add("00:00:00")
                mylist2.Add(ls)
            Else
                query = "Select Count(*), Max(logoutTime) as logoutTime FROM tbl_login WHERE agentName='" & mylist1(i)(0).ToString() & "' AND logoutDate ='" & mylist1(i)(3).ToString() & "' AND loginDate = '" + DateTimePicker1.Text + "'"
                cmd = New MySqlCommand(query, SQLConnection)
                count = cmd.ExecuteScalar()
                If count >= 1 Then
                    Using readers As MySqlDataReader = cmd.ExecuteReader()
                        While readers.Read()
                            Dim ls As New List(Of String)
                            Dim timeOut As Date = readers.GetString(1)
                            actualLogoutTime = timeOut.ToString("HH:mm:ss")
                            ls.Add(actualLogoutTime)
                            mylist2.Add(ls)
                        End While
                    End Using
                Else
                    Dim ls As New List(Of String)
                    ls.Add("00:00:00")
                    mylist2.Add(ls)
                End If
            End If
Next

1 个答案:

答案 0 :(得分:0)

请注意,12:49是下午1点(下午1点)之前的下午12点49分,00:49正好是凌晨1点(上午1点)。您可能只是遇到数据输入问题。