checking if a selected time is in the past

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Hi,

I think this is fairly simple, but then isn't everything when you know how. I would like to populat a listbox with some time periods.
For instance

10:00-12:00
12:00-14:00
14:00-16:00
16:00-18:00
18:00-20:00

One this is done I would like to check that the selected item in the listbox is not in the past.
So if the current time is 12:00 then the user will not be able to select 10:00-12:00.

Thats my first question. My 2nd question is - in what format would time be stored in a database?

Basically I need to know how to compare the system time with the time in the listbox.

Thanks for any help.
Levy
 

johnminkjan

Member
Joined
May 21, 2005
Messages
5
Location
the Netherlands
Programming Experience
5-10
1. Try This:
VB.NET:
PrivateSub cmdLoadLbx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoadLbx.Click
  
Me.lbxTimeRangeToCheck.Items.Add("00:00-02:00")
 
Me.lbxTimeRangeToCheck.Items.Add("02:00-04:00")
 
Me.lbxTimeRangeToCheck.Items.Add("04:00-06:00")
 
Me.lbxTimeRangeToCheck.Items.Add("06:00-08:00")
 
Me.lbxTimeRangeToCheck.Items.Add("08:00-10:00")
 
Me.lbxTimeRangeToCheck.Items.Add("10:00-12:00")
 
Me.lbxTimeRangeToCheck.Items.Add("12:00-14:00")
 
Me.lbxTimeRangeToCheck.Items.Add("14:00-16:00")
 
Me.lbxTimeRangeToCheck.Items.Add("16:00-18:00")
 
Me.lbxTimeRangeToCheck.Items.Add("18:00-20:00")
 
Me.lbxTimeRangeToCheck.Items.Add("20:00-22:00")
 
Me.lbxTimeRangeToCheck.Items.Add("22:00-23:59")
 
EndSub
 
PrivateSub cmdCheckTimeFrame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckTimeFrame.Click
 
Dim chkTime AsDate
 
'check if range is selected
 
IfMe.lbxTimeRangeToCheck.SelectedItem = ""Then
 
MsgBox("no timeframe seleceted")
 
ExitSub
 
EndIf
 
chkTime = CDate("#" & (Mid(Me.lbxTimeRangeToCheck.SelectedItem, 7, 4)) & "#")
 
If chkTime < TimeOfDay Then
 
MsgBox("Current Time is past time frame")
 
EndIf
 
EndSub
2. Date type

HTH

John
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,321
Location
Sydney, Australia
Programming Experience
10+
It would create a better user experience if invalid time periods did not even appear in the ListBox. Why let the user select something that you know ahead of time is going to be invalid. I would suggest something like this:
VB.NET:
If DateTime.Now < DateTime.Today.AddHours(2) Then
   Me.ListBox1.Items.Add("00:00 - 01:59")
 End If
If DateTime.Now < DateTime.Today.AddHours(4) Then
   Me.ListBox1.Items.Add("02:00 - 03:59")
 End If
If DateTime.Now < DateTime.Today.AddHours(6) Then
   Me.ListBox1.Items.Add("04:00 - 05:59")
End If
The passing of time may make a period invalid while the ListBox is visible, which you would have to handle using code like that posted by johnminkjan, but try to give your users as few invalid options as possible.
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Yes I was going to move ono that after I got the comparing correct. The problem is that I have a list off times in a database. I don't add them programmatically. I just do a select then populat the listbox. If you can suggest a way to get the valid times from the database that would help a lot.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,321
Location
Sydney, Australia
Programming Experience
10+
Are your times stored in the database as actual times as opposed to a string like "10:00 - 12:00"? If so you could use your select statement to return only times later than the current time. Using Access it would look something like:

"SELECT TimeField FROM TimeTable WHERE TimeField > TimeValue('" & DateTime.Now & "')"

I've not used TimeValue() before, only DateValue(), but I think this is correct. Other databases should have equivalents.
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
They are stored as text, I don't know how I would store them as time in SQL Server. Do you know?

If not how could I do it?
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Yes we seem to do that quite a bit. I'm looking at yours now to see if I can modify it. I'll add another issue to it now though :)

The time is coupled with the date. For instance - if the day is tomorrow then the time cannot be in the past because it is tomorrow. So I have to check that aswell. Because people can make orders in the future then I wont need to check the time, which I should be able to do myself, I think. I shall let you know.
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Ok thanks guys for the help, I have it working now. After much stress I might add.
 
Top Bottom