VB.Net. Resolving different date formats

Hi Experts.  I currently enter a date into a textbox with this

Textbox1.Text = Now.ToLongDateString

I then save this to a SQLCE Database as text which saves like this

08 February 2015

When I call the text back from the database with this

Dim DateFromDB As DateTime = DateTime.Parse(DBString(3))

I get the date as this

2015/02/08

I then use the dates like this

If DateFromDB.Date >= StartDate.Date And DateFromDB.Date <= EndDate.Date Then

This seems to work correctly on some clients but not at all on others.
I suspect it has to do with the date format on the client.
Does VB.Net not automatically allow or compensate for different date formats like this.  Am I supposed to convert to a standard format?
Any help would be appreciated.
Many Thnaks
LVL 2
PNRTAsked:
Who is Participating?
 
Jacques Bourgeois (James Burger)PresidentCommented:
If you handle the date as text, you will have lots and lots of problems when you move from one computer to another, because many will have different text formats for the date. This is defined in the computer Control Panel, and each user will have its preferences.

To prevent these problems, you should always handle dates as DateTime or Date in your code and in the database. In these types, the date is handled by a number and is thus completely independent of the string representation and of the computer on which you are running. The date will be the same, no matter where you use it.

You should use the different versions of ToString only for display purposes. These will automatically adjust the format to what is defined in the Control Panel, so the user is not mixed up with dates that have a given format in Excel, another one in his accounting software, and a third one in your application. When everyone uses ToString (and everyone should), the display format will be the same everywhere on a given computer.

The reverse is also true. As long as the user types the date in the format defined in his Control Panel, you can convert the content of a TextBox to a standardized Date by using CDate or Date.TryParse.

Here is a short example:

Dim d As Date
d=CDate(TextBox1.Text)

Date.TryParse is more interesting because it validates the date, something that CDate does not do. It's a little more complex to use, but is worth it. Look it up in the documentation for more information and for examples of how to use it.
0
 
David Johnson, CD, MVPOwnerCommented:
you must always use a datetime object and not mix or convert them to strings .. if you save items at text then they will use the regional settings of the client
0
 
PNRTAuthor Commented:
Thank you for your reply.   If I save to the database as a datetime object, does it matter what format the date is originally in?  Can it be any format and it is saved and retrieved correctly if it is a datetime object?   I'm wondering if I need to change to format that is displayed in the textbox before converting it back to a date?
0
 
David Johnson, CD, MVPOwnerCommented:
Dim StrDate   as datetime
StrDate = now
Textbox1.Text = StrDate.ToLongDateString


I then save this to a SQLCE Database as text which saves like this
You are saving a string!
you should be saving StrDate not the TextBox1.Text
0
 
PNRTAuthor Commented:
As usual Jacques, many thanks for the very full explanation, especially the last part
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.