Click here to Skip to main content
15,887,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code for my BindingSource.Filter

VB
Dim FilterStartMonth As Date = DateTimePickerTodaysDateTime.Value.Date 
Dim Filter2MonthsBack As Date = DateTimePickerTodaysDateTime.Value.AddMonths(-2).Date 

ClockInTimesBindingSource.Filter = "EmployeeID = " & ComboBoxOfficeEmployeeFilter.SelectedValue & " and Date <= '" & Filter2MonthsBack & "' and Date >= '" & FilterStartMonth & "'"


When i go through it step by step as it runs these are the values:

Filter2MonthsBack = 1/2/2017 12:00:00 AM

FilterStartMonth = 3/2/2017 12:00:00 AM

Which is how i want them, but the BindingSource.Filter reads:


ClockInTimesBindingSource.Filter = "EmployeeID = 49 and Date <= '02/01/2017' and Date >= '02/03/2017'"

I can't work out why it is swapping the month and day around??

Any help would be much appreciated.

What I have tried:

VB
Dim FilterStartMonth As Date = DateTimePickerTodaysDateTime.Value.Date.ToString("d") 
Dim Filter2MonthsBack As Date = DateTimePickerTodaysDateTime.Value.AddMonths(-2).Date.ToString("d") 

ClockInTimesBindingSource.Filter = "EmployeeID = " & ComboBoxOfficeEmployeeFilter.SelectedValue & " and Date <= '" & Filter2MonthsBack & "' and Date >= '" & FilterStartMonth & "'"


VB
Dim FilterStartMonth As Date = DateTimePickerTodaysDateTime.Value.Date 
Dim Filter2MonthsBack As Date = DateTimePickerTodaysDateTime.Value.AddMonths(-2).Date 

ClockInTimesBindingSource.Filter = "EmployeeID = " & ComboBoxOfficeEmployeeFilter.SelectedValue & " and Date <= '" & Filter2MonthsBack.ToString("d") & "' and Date >= '" & FilterStartMonth.ToString("d") & "'"


VB
Dim FilterStartMonth As Date = DateTimePickerTodaysDateTime.Value.Date.ToString("d") 
Dim Filter2MonthsBack As Date = DateTimePickerTodaysDateTime.Value.AddMonths(-2).Date.ToString("d") 

ClockInTimesBindingSource.Filter = "EmployeeID = " & ComboBoxOfficeEmployeeFilter.SelectedValue & " and Date <= '" & Filter2MonthsBack.ToString("d") & "' and Date >= '" & FilterStartMonth.ToString("d") & "'"
Posted
Updated 2-Mar-17 13:04pm

1 solution

Your system Culture settings usually deturmines the ToString format. You can check it by doing the following:
VB
Dim culture = Globalization.CultureInfo.CurrentCulture
Dim CultureName = culture.Name
Dim shortDateFormatString = culture.DateTimeFormat.ShortDatePattern
Dim longDateFormatString = culture.DateTimeFormat.LongDatePattern
If you want to force the formatting of dates, then you can pass a formate to ToString as it is overloaded to support custom formatting:
VB
Dim reversedDate = Date.Now.ToString("yy-dd-mm") ' 3/2/17 becomes 17-2-3
You can read more about it here: Custom Date and Time Format Strings[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900