Get date from textbox

D

Dave

Hi,
I have a textbox that the user inserts the date of birth in the format
dd/mm/yyyy, I'm validating it with regex

to set the value to a Date variable, I have

dBithDay = CDate(txtBDate.txt)

but if the user enters 01/02/1980, the month of the date variable is 01
instead of 02.

should I use substring??

Thanks!
 
Q

q

You could just use this...


string dateString = "01/02/1980";
DateTime date;
if (DateTime.TryParse(dateString, out date)) {
string month = date.Month;
string year = date.Year;
string day = date.Day;
}
else {
// Bad...
}
 
B

Bruno Piovan

You can do this way...

Dim dateString As String = "01/02/1980"
Dim dtfi As New System.Globalization.DateTimeFormatInfo
dtfi.ShortDatePattern = "dd/MM/yyyy"
Dim d As Date = Date.Parse(dateString, dtfi)

you can create an object of type CultureInfo and use it instead of
DateTimeFormatInfo

Bruno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top