Jump to content
Xtreme .Net Talk

Parsing user date input


Recommended Posts

Guest adouglas
Posted

I've got a textbox into which the user can enter a date. Whenever I try to convert the text from this textbox into a Date using Date.Parse or CDate, it insists on reading it as Month/Day/Year, even though my PC's regional settings are set up to be Day/Month/Year. How can I make VB.NET read the date properly without getting the Day and Month mixed up?

 

Thanks.

Posted

You can use the format function in VB.NET with the combination of a format string. You have to look in the help of VB.NET to know exactly the string. It's something like "d" or "dd" and so on.

 

But then you will be able to convert your date into the right setting.

Visit http://www.nico.gotdns.com

 

Now ONLINE!

Guest adouglas
Posted
No, that's not the problem. Format is used for outputting a date in the format you specify. My problem is reading in a date. The user enters a date into a textbox, and I need to read it into a date variable. The trouble is, all the functions I can find which will do this seem to assume that the date is being entered in month/day/year format, so if the user enters "9/8/2002", the date variable that gets created is 8th September, 2002, when it should actually be 9th August, 2002. I'm pretty sure VB6 used the regional settings on your computer to determine what format to read the date as, but VB.NET seems to assume that the entire world is using the American date format instead of looking in the regional settings.
Guest adouglas
Posted

Thanks to both of you for replying. I actually just found the solution this very minute. Instead of

 

Date.Parse(datestring)

 

I needed to use

 

Date.Parse(datestring, System.Globalization.Cultureinfo. CurrentCulture). This CurrentCulture object seems to contain all the regional settings related information.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...