Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Format Strings?

Status
Not open for further replies.

qsysoper

Programmer
Mar 20, 2003
7
US
How can you format an actual string in V.NET?

In VB6 I'd do this:
format("010102", "00/00/00")
and get this:
"01/01/02"

In vb.NET this:
format("010102", "00/00/00")
gets this:
"00/00/00"

Now I know that making the first arg a date or numeric variable would work, but I need to format strings sometimes, not always other data types.

So what is the standard method in VB.NET for applying formatting masks to strings?

Thanks!
 
format("010102", "MM/dd/yy")
 
Hmm, probably not the best way to do this, I'm sure there's a much easier/efficient way - but it works none the less.

Code:
MsgBox(Date.FromOADate(CDbl("010102") + 27155))

This returns "01/01/02"

Pete

There's a thin line between genius, and insanity!
 
just realized what i posed doesnt work, i could have sworn i have done this before
 
Hi Blitz,

Yeah - it's what the MSDN help files say too.. but all I get is literally "MM/dd/yy". Strange!

Pete

There's a thin line between genius, and insanity!
 
Code:
Dim date As DateTime = DateTime.ParseExact("010102", "MMddyy", System.Globalization.DateTimeFormatInfo.CurrentInfo)

You can then use the various .To*Whatever* methods of the date variable to format your output however you want it.

NOTE: the MM must be capitalized. mm is minutes.

NOTENOTE: I didn't actually test this in VB as I am using C#, but it should work the same assuming my declaration is typo free.
 
gsysoper:
So what is the standard method in VB.NET for applying formatting masks to strings?

The *ahem* standard method in .NET is to use the Parse methods of the type you are converting to. There may be other shortcuts (?) but a) I'm not aware of them and b) they would probably defeat the purpose of .NET being object-oriented if there are.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top