By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 2,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

format string from 8 to 08 for August.

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.