We help IT Professionals succeed at work.

right function. Translation from VB 6 to .NET

pvg1975
pvg1975 used Ask the Experts™
on
Hi all,

How do I translate the following in ASP.NET?

XMonth= Right("00" & Month(now()), 2)

I tried

Dim XMonth as String = Right("00" & Month(Today), 2)

but its giving me an error on the right function

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
XMonth= Right("00" & Month(Today), 2)
Well VB.NET ported all of those old-style function over, so your code still technically is valid. But if you're interested in making it more .NET-centric, then you could do something like:

XMonth = Today.Month.ToString("00")

Open in new window

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
When I say, "your code still technically is valid," I am referring to the old VB6 code (i.e. your first snippet), not the one I quoted. Sorry if it caused confusion.

Author

Commented:
Hey K

That will result in, for example 12 for december, 05 for may (not 5), 01 for june, etc?

Thanks!

Author

Commented:
Sorry I mean 01 for January :P
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
That will result in, for example 12 for december, 05 for may (not 5), 01 for june, etc?
Yes. Looking at the VB6 code, I understood that to be what you were after.

The "00" part of the ToString call is what ensures you have a leading zero if one doesn't exist. You could also do:
XMonth = Today.Month.ToString().PadLeft(2, "0"c)

Open in new window

Author

Commented:
I cannot try it now, that's why I ask BTW :)

Author

Commented:
Thanks! I will try it as soon as Im back home. Thanks again! :)
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
No problem. Glad to help  = )

P.S.

Feel free to post back if you have any questions about the above.