Solved

String.Format for Month numeric?

Posted on 2008-10-31
7
544 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 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