Solved

String.Format for Month numeric?

Posted on 2008-10-31
7
536 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
The Using statement in C# 5 36
Linq help 1 16
Duplicate a row 2 33
install app on other machine 13 13
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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

21 Experts available now in Live!

Get 1:1 Help Now