473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 9913
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kevininstructor | last post by:
The following was copied from a web site for learning formatting. Dim myInt As Integer = 100 Dim FormatPrice As String = String.Format("Price = |{0,10:c }|", myInt) Console.WriteLine(FormatPrice)...
6
by: Servé Lau | last post by:
suppose I want to use sscanf get the functionname from a function prototype. Is the following format string correct then? char funcname; char *p = "func(void)"; sscanf(p, "%s", funcname);...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
2
by: MM | last post by:
Hi, I have a param class containg these vars:- string key; // eg: "WN" object value; // eg: 1.2 string format; // eg "F2" and I output these to a StreamWriter using ...
7
by: Eddy Soeparmin | last post by:
Hi, I need to display a DateTime field in 'mm/dd/yyyy' in a DataGrid.. On myGrid1 - Properties - Columns - myColumn1 - Text format string: I tried to put 'mm/dd/yyyy' in there and it displays...
4
by: James | last post by:
vb.net 2003 i used console.writeline to output to screen. eg console.writeline ("test1 : " & vbtab & v_test1) console.writeline ("test2 : " & vbtab & v_test2) etc etc result becomes
3
by: stathisgotsis | last post by:
Hello everyone, Trusting K&R2 i thought until recently that spaces are ignored in scanf's format string. Reading arguments to the contrary confused me a little. So i now ask: Is...
7
by: Tony Girgenti | last post by:
Hello. Trying to develop VS2005, SP1 windows form VB program. When i try a statement like below and the data is "24.95", without the quotes of course, regPriceString equals "000000000025+" . ...
7
by: Andrus | last post by:
How to create format string for decimal data type which shows blank for zero and default format otherwize ? I tried format string "f;f;#" but this shows f for nonzero numbers. Andrus. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.