Solved

String.Format for Month numeric?

Posted on 2008-10-31
7
542 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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