Solved

String.Format for Month numeric?

Posted on 2008-10-31
7
539 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 142

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now