Can someone help me to format this.
in my textbox (The date is 10222012)
the output should be (The date is 10/22/2012)
the text should be still there
thank you
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A couple of approaches off the top of my head.
If the text is coming from one variable, for example:
Then you can use String functions to get the date (assuming it is always 8 characters, which can be a dangerous assumption), format it and stick it back in:
If on the other hand the date is in a separate variable, say a DateTime variable, you can do this:
There are other ways to do this. The key here is to get the date and convert it to the format you want via the DateTime.ToString Method (String) method, where the format string in the parantheses tells what format to use.
EDITED
That’s what I get for not trying the code out first…if the string “10222012” isn’t recognized as a valid format, there’s another way to do this:
It’s a bit more brute force and ugly, but that should get you going in the right direction. You might be able to get the same result use
Insert(another String operator), but that might be just as ugly.If you need to do this conversion a lot, I recommend putting it into a helper method, or better yet an extension method.
2nd Edit
You can check for blank record easily enough: