471,338 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

format string from 8 to 08 for August.

I need to format a string to it always has 2 digits in it. I'm getting the
month like this:

DateTime.Now.Month.ToString()

Right now since it's August, this returns a string of "8", however, I need
to return a string of "08". Is there a format method what will
automatically pad this for me?

Thanks.
--
mo*******@newsgroup.nospam
Aug 22 '08 #1
7 9757
On Aug 22, 3:19*pm, "moondaddy" <moonda...@newsgroup.nospamwrote:
I need to format a string to it always has 2 digits in it. *I'm gettingthe
month like this:

DateTime.Now.Month.ToString()

Right now since it's August, this returns a string of "8", however, I need
to return a string of "08". *Is there a format method what will
automatically pad this for me?

Thanks.

--
moonda...@newsgroup.nospam

use DateTime.Now.ToString("MM");

take a look at the different DateTime format strnig options
Aug 22 '08 #2
On Aug 22, 3:33*pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Aug 22, 3:19*pm, "moondaddy" <moonda...@newsgroup.nospamwrote:
I need to format a string to it always has 2 digits in it. *I'm getting the
month like this:
DateTime.Now.Month.ToString()
Right now since it's August, this returns a string of "8", however, I need
to return a string of "08". *Is there a format method what will
automatically pad this for me?
Thanks.
--
moonda...@newsgroup.nospam

use DateTime.Now.ToString("MM");

take a look at the different DateTime format strnig options
Or string.Format("{0:D2}", DateTime.Now.Month)
Aug 22 '08 #3
za***@construction-imaging.com wrote:
On Aug 22, 3:33 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
>On Aug 22, 3:19 pm, "moondaddy" <moonda...@newsgroup.nospamwrote:
>>I need to format a string to it always has 2 digits in it. I'm getting the
month like this:
DateTime.Now.Month.ToString()
Right now since it's August, this returns a string of "8", however, I need
to return a string of "08". Is there a format method what will
automatically pad this for me?
use DateTime.Now.ToString("MM");

take a look at the different DateTime format strnig options

Or string.Format("{0:D2}", DateTime.Now.Month)
But the .ToString seems both more readable and more
flexible.

Arne
Aug 22 '08 #4
On Aug 22, 4:49*pm, Arne Vajhřj <a...@vajhoej.dkwrote:
za...@construction-imaging.com wrote:
On Aug 22, 3:33 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Aug 22, 3:19 pm, "moondaddy" <moonda...@newsgroup.nospamwrote:
I need to format a string to it always has 2 digits in it. *I'm getting the
month like this:
DateTime.Now.Month.ToString()
Right now since it's August, this returns a string of "8", however, Ineed
to return a string of "08". *Is there a format method what will
automatically pad this for me?
use DateTime.Now.ToString("MM");
take a look at the different DateTime format strnig options
Or string.Format("{0:D2}", DateTime.Now.Month)

But the .ToString seems both more readable and more
flexible.

Arne
You can do it either way, it formats the string the way your format
string specifies.

Read up on string formatting. It's very interesting. Try experimenting
with string formatting by using the string.Format() function. There
are lists of format codes on the web, just google for format string C#.
Aug 23 '08 #5
maximz2005 wrote:
On Aug 22, 4:49 pm, Arne Vajhřj <a...@vajhoej.dkwrote:
>za...@construction-imaging.com wrote:
>>On Aug 22, 3:33 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Aug 22, 3:19 pm, "moondaddy" <moonda...@newsgroup.nospamwrote:
I need to format a string to it always has 2 digits in it. I'm getting the
month like this:
DateTime.Now.Month.ToString()
Right now since it's August, this returns a string of "8", however, I need
to return a string of "08". Is there a format method what will
automatically pad this for me?
use DateTime.Now.ToString("MM");
take a look at the different DateTime format strnig options
Or string.Format("{0:D2}", DateTime.Now.Month)
But the .ToString seems both more readable and more
flexible.

You can do it either way, it formats the string the way your format
string specifies.
No one has denied that.

Arne
Aug 23 '08 #6
moondaddy wrote:
I need to format a string to it always has 2 digits in it. I'm getting the
month like this:

DateTime.Now.Month.ToString()

Right now since it's August, this returns a string of "8", however, I need
to return a string of "08". Is there a format method what will
automatically pad this for me?

Thanks.
Here are some ways of doing it (including the ones already mentioned in
the thread):

DateTime.Now.Month.ToString("00")
DateTime.Now.Month.ToString("D2")
DateTime.Now.ToString("MM")
string.Format("{0:00}", DateTime.Now.Month)
string.Format("{0:D2}", DateTime.Now.Month)
string.Format("{0:MM}", DateTime.Now)

--
Göran Andersson
_____
http://www.guffa.com
Aug 24 '08 #7
Thanks Göran and everyone else who replied. this has all been very helpful.
"Göran Andersson" <gu***@guffa.comwrote in message
news:uG**************@TK2MSFTNGP04.phx.gbl...
moondaddy wrote:
>I need to format a string to it always has 2 digits in it. I'm getting
the month like this:

DateTime.Now.Month.ToString()

Right now since it's August, this returns a string of "8", however, I
need to return a string of "08". Is there a format method what will
automatically pad this for me?

Thanks.

Here are some ways of doing it (including the ones already mentioned in
the thread):

DateTime.Now.Month.ToString("00")
DateTime.Now.Month.ToString("D2")
DateTime.Now.ToString("MM")
string.Format("{0:00}", DateTime.Now.Month)
string.Format("{0:D2}", DateTime.Now.Month)
string.Format("{0:MM}", DateTime.Now)

--
Göran Andersson
_____
http://www.guffa.com

Aug 25 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by kevininstructor | last post: by
6 posts views Thread by Servé Lau | last post: by
6 posts views Thread by Donal McWeeney | last post: by
2 posts views Thread by MM | last post: by
7 posts views Thread by Eddy Soeparmin | last post: by
4 posts views Thread by James | last post: by
3 posts views Thread by stathisgotsis | last post: by
7 posts views Thread by Tony Girgenti | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.