Forum Moderators: open

Message Too Old, No Replies

date formatting in vb .net

trying to convert string to date

         

jefferson

9:28 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



I have a textfield called txtDOB. takes the string variable, and converts it into a date. want to know how i can test the string before converting it to make sure it is in MM/DD/YYYY format. OR, not very good with error handling. how about making an error handler that tells user that it is not a correct date format.

TheNige

9:44 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



Well, you can actually test it by using the asp validators...they have one that checks for dates....no matter what format they type it in.

Use the compare validator, set the operator to DataTypeCheck and the Type=Date.

You could then just take the verified string and insert it into your database in what ever format you want.

CaseyRyan

10:31 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



VB.Net has an ISDate() [msdn.microsoft.com] function that would confirm the value is a date.

-=casey=-

defanjos

10:39 pm on Apr 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One thing I do, is to always have 3 fields for the date - month (dropdown), day (dropdown) and year (dropdown). Then, I simply concatenate the 3 fields.

This way the data is always entered correctly into the db.
Furthermore, if you use dropdowns, there is no way they can screw it up.

mattglet

2:03 am on Apr 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Casey-

I don't think IsDate() is a proper function to use in .NET. Kind of defeats the purpose of OOP, no?

Just my opinion though.

CaseyRyan

2:44 am on Apr 8, 2005 (gmt 0)

10+ Year Member



Matt,
why reinvent the wheel? IsDate() will do what jefferson was asking for.

-=casey=-

Easy_Coder

3:52 am on Apr 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not just replace your textbox control with a calendar control? Then on the client utilize the validators that TheNige mentioned? On the server put yourself together a regex to validate the date formate if IsDate no worky for you.

mattglet

1:53 pm on Apr 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While I agree there's no reason to reinvent the wheel, we're actually NOT reinventing the wheel here. We're using the .NET framework, not VBScript. If people are going to learn to develop using .NET, I think it's only right to TEACH .NET, and not just feed them antiquated VBScript functions. So many people don't use the new technology to their advantage (or correctly, for that matter), and that's a sad thing.

This is not a personal attack at all, forgive me if you see as such. It's more of a general rant of mine.

CaseyRyan

3:34 am on Apr 11, 2005 (gmt 0)

10+ Year Member




While I agree there's no reason to reinvent the wheel, we're actually NOT reinventing the wheel here. We're using the .NET framework, not VBScript. If people are going to learn to develop using .NET, I think it's only right to TEACH .NET, and not just feed them antiquated VBScript functions. So many people don't use the new technology to their advantage (or correctly, for that matter), and that's a sad thing.
This is not a personal attack at all, forgive me if you see as such. It's more of a general rant of mine.

Matt, if this wasn't a personal attack, why didn't you just post your solution instead of singling me out of the other people who had posted? Don't you think that would have been a little more appropriate than a rant and lecture?

I stand by my original post. I know that I could use regular expressions in conjunction with some other parts of the .net framework to solve the problem, but I've found the simplest solution is often the best. I, like others, need to save time wherever I can when writing code. If it's antiquated, I don't care. It produces the same result in the end.

You should realize that in programming, there's a tons of ways to do things. I appreciate that you feel your way is the "right" way. I've been there, but I don't think that these forums are what that is about. People post questions and other people help them out by posting possible solutions. There's always going to be solutions that aren't optimal or are flawed, but the best teaching comes from seeing all of those different approaches. How else do you learn?

Anyway, I think we've used this simple date validation thread for something more than it was intended. I wish to hijack it no further. If you'd like to continue this discussion, I suggest you sticky me.

Jefferson, I hope you've found your solution.

-=casey=-

mrMister

12:14 pm on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't you think that would have been a little more appropriate than a rant and lecture?

He was only pointing it out, so that the user could understand the issues.

It's best to use good coding practices at all times when trying to help others on forums, it helps prevent them picking up bad habits.

When you suggest something that isn't best practice then it's wise to inform the recepient of that so that they can make an informed choice.