dateTimePicker - change time

banks

Well-known member
Joined
Sep 7, 2005
Messages
50
Programming Experience
Beginner
Hi, I am using a dateTimePicker to select dates so that they can be used in criteria for a query. The problem i'm having is that when i select a date, the current time comes with it, so when searching on the database if a record has say inserted with a date of 01/01/2006 12:04:33, i may search on the date but it will take the value 01/01/2006 12:36:33(the current time), which means i miss out on my record. Is there a way to set the time on a date to 00:00:00 and equally on the to date to 23:59:59? This is the code i use to get my date value into a variable to use with my insert or select statement - btw i am using Access 97. Dim dDate As Date = dtpDeliveryDate.Text Many thanks, Alex
 
You shouldn't be using the Text property of a DateTimePicker. The Text property is a String. The Value property is a Date object and it's that property you should be using. If you want just the date part of a Date object you use its Date property. That means that if you have two DTPs (one for the start date and one for the end date) you would get the appropriate Date objects like so:
VB.NET:
Dim startDate As Date = startDatePicker.Value.Date 'Gets just the date portion by zeroing the time.
Dim endDate As Date = endDatePicker.Value.Date.AddDays(1).AddSeconds(-1)
The second line zeroes the time, then increments the day and then subtracts one second, so you get 23:59:59 on the date originally selected.

Alternatively you could just set the Value properties of the two DTPs appropriately in the first place and then just use the Value properties directly:
VB.NET:
Dim today As Date = Date.Today 'Today's date with the time zeroed.

startDatePicker.Value = today
endDatePicker.Value = New Date(today.Year, today.Month, today.Day, 23, 59, 59)
If do that then it doesn't matter what the user changes the dates to. The time portions of the Value properties will still be 00:00:00 and 23:59:59, so you can just use the Value property of each DTP directly.
 
Back
Top