×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

String.Format to get Month Number

String.Format to get Month Number

String.Format to get Month Number

(OP)
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

RE: String.Format to get Month Number

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.

RE: String.Format to get Month Number

@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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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! Already a Member? Login

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