Solved

String.Format for Month numeric?

Posted on 2008-10-31
7
541 Views
Last Modified: 2012-05-05
Experts,

I'm try to output the months of year in the following format:

01
02
03
04
... and so on

DateTime gives me:

1
2
3
3
... for the months

I'd like to start using String.Format method, and have taken an example.

DateTime date = System.DateTime.Now;
string monthFormatted = String.Format("{0:dd}", date);

This doesnt work, please help!
0
Comment
Question by:mhouldridge
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22848742
what about:
DateTime date = System.DateTime.Now;
string monthFormatted = date.Month.ToString("00");

Open in new window

0
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22848746
use this
DateTime date = System.DateTime.Now;

Console.WriteLine(date.ToString("dd",
                  CultureInfo.InvariantCulture));
0
 
LVL 13

Accepted Solution

by:
kaylanreilor earned 500 total points
ID: 22848752
String.Format("{0:MM}", date);
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22848757
for month use this
DateTime date = System.DateTime.Now;

Console.WriteLine(date.ToString("MM",
                  CultureInfo.InvariantCulture));
0
 

Author Comment

by:mhouldridge
ID: 22848784
No,

None of these work for me.  I'd have thought this would be right, because I was using dd (day) instead of MM (month).

String.Format("{0:MM}", date);

thanks
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 22848801
Do you mean that String.Format("{0:MM}", date); doesn't work ???

If yes, want does the debugger say about mystring value in a watch window after this statement :
String mystring = String.Format("{0:MM}", date);
0
 

Author Comment

by:mhouldridge
ID: 22848816
Sorry,

That one was correct.  I had to Rebuild the solution, as build gave me the previous output.

Many thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question