473,795 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Mo nth.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*******@newsg roup.nospam
Aug 22 '08 #1
7 9950
On Aug 22, 3:19*pm, "moondaddy" <moonda...@news group.nospamwro te:
I need to format a string to it always has 2 digits in it. *I'm gettingthe
month like this:

DateTime.Now.Mo nth.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...@newsg roup.nospam

use DateTime.Now.To String("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...@news group.nospamwro te:
I need to format a string to it always has 2 digits in it. *I'm getting the
month like this:
DateTime.Now.Mo nth.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...@newsg roup.nospam

use DateTime.Now.To String("MM");

take a look at the different DateTime format strnig options
Or string.Format(" {0:D2}", DateTime.Now.Mo nth)
Aug 22 '08 #3
za***@construct ion-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...@news group.nospamwro te:
>>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
automatical ly pad this for me?
use DateTime.Now.To String("MM");

take a look at the different DateTime format strnig options

Or string.Format(" {0:D2}", DateTime.Now.Mo nth)
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.d kwrote:
za...@construct ion-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...@news group.nospamwro te:
I need to format a string to it always has 2 digits in it. *I'm getting the
month like this:
DateTime.Now.M onth.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
automaticall y pad this for me?
use DateTime.Now.To String("MM");
take a look at the different DateTime format strnig options
Or string.Format(" {0:D2}", DateTime.Now.Mo nth)

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.d kwrote:
>za...@construc tion-imaging.com wrote:
>>On Aug 22, 3:33 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac. ..@gmail.comwro te:
On Aug 22, 3:19 pm, "moondaddy" <moonda...@news group.nospamwro te:
I need to format a string to it always has 2 digits in it. I'm getting the
month like this:
DateTime.No w.Month.ToStrin g()
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
automatical ly pad this for me?
use DateTime.Now.To String("MM");
take a look at the different DateTime format strnig options
Or string.Format(" {0:D2}", DateTime.Now.Mo nth)
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.Mo nth.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.Mo nth.ToString("0 0")
DateTime.Now.Mo nth.ToString("D 2")
DateTime.Now.To String("MM")
string.Format(" {0:00}", DateTime.Now.Mo nth)
string.Format(" {0:D2}", DateTime.Now.Mo nth)
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.co mwrote in message
news:uG******** ******@TK2MSFTN GP04.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.M onth.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
automaticall y pad this for me?

Thanks.

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

DateTime.Now.Mo nth.ToString("0 0")
DateTime.Now.Mo nth.ToString("D 2")
DateTime.Now.To String("MM")
string.Format(" {0:00}", DateTime.Now.Mo nth)
string.Format(" {0:D2}", DateTime.Now.Mo nth)
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
1510
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) The result shown is no number but the character c as in the format string. Is the format not valid in this context? Thanks for your assistance, Kevin
6
3405
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); // "%s"
6
2613
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 3.21 will display 3.21
2
1582
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 nc_file.Write("{0}{1:" + param.format + "}", param.key, param.value);
7
7669
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 'mm/dd/yyyy' in stead of '04/10/2001'. I know I didn't do it right. Please let me know what's the correct way to display a 'mm/dd/yyyy' format for a DateTime coulmn. Thanks.
4
6997
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
12801
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 scanf("%d%d",...) different from scanf("%d %d",...) in the Standard's point of view? Thank you.
7
5480
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+" . regPriceString = String.Format("{0:000000000000+}", merchandiseDataRow.Item("RegPrice"))
7
13691
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. using System.Windows.Forms;
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.