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


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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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?
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
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

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PNRTAuthor Commented:
As usual Jacques, many thanks for the very full explanation, especially the last part
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.