Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Since using forums in my early days 10 years ago in CompuServe, one had to log back on and sometimes wait days for a response. Now I get a response e-mailed to me which I can click a link and go right back to exactly where My post was..."

Geography

Where in the world do Tek-Tips members come from?
tshad (Programmer)
25 Jul 12 20:20
I need to get the month number from a date using String.Format. But it doesn't seem to work correctly.

string.Format("{0:M}", entity.OrderPeriodDate)

Comes back with "June 15" and not 6 as many posts say.

string.Format("{0:MM}", entity.OrderPeriodDate)

Comes back with "06".

I need to get it without the leading zero. "M" is supposed to do that.

How can I get this correctly?

Thanks,

Tom
DansDadUK (Programmer)
26 Jul 12 5:47
The 'M' is being interpreted as a standard (not custom) date format specifier.

The specifications state:

The "M" custom format specifier represents the month as a number from 1 through 12 (or from 1 through 13 for calendars that have 13 months). A single-digit month is formatted without a leading zero.

If the "M" format specifier is used without other custom format specifiers, it is interpreted as the "M" standard date and time format specifier.


To use any of the custom date and time format specifiers as the only specifier in a format string (that is, to use the "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" custom format specifier by itself), include a space before or after the specifier, or include a percent ("%") format specifier before the single custom date and time specifier.
elconomeno (TechnicalUser)
8 Aug 12 7:34
i give it a try :

DateTime date;
int month = date.Month;
month.toString();

http://elconomeno.blogspot.com/

Qik3Coder (Programmer)
8 Aug 12 12:31
@elconomeno,

You're on the right path. If he just needs the month, then String.format() is over kill. You can collapse your solution down further.

CODE

entity.OrderPeriodDate.Month.ToString(); 

Lodlaiden

You've got questions and source code. We want both!
There's a whole lot of Irish in that one.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close