6

I'm working on a project where I need to format a string with a bunch of stuff but the pertinent part is the first part - the time of a tour:

Given

DateTime start = Convert.ToDateTime(myClass.StartDateTime);

How do I output it with string.Format to show either 10:00 or < space >3:00?

I know I can use a format string like:

string  text = string.Format("{0:hh:mm tt}",_start);

but that gives me a leading zero (03:00 PM) that I don't want for single digit hours.

Is there something I can add into the format string to do this or do I have to manually substitute a space for a leading zero? (I suspect this is the case but wanted to ask so I can learn how if there is a format method.)

3 Answers 3

6
string  _text = string.Format("{0:h:mm tt}",_start).PadLeft(8, ' ');

should do it.

The single "h" in the Time Format String will make it so that the zero doesn't show if it is a single digit hour,l and the PadLeft function will ensure that it is the proper width.

2
  • 2
    You could also use an alignment component in the format string to make string.Format do the padding for you: string _text = string.Format("{0,8:h:mm tt}", _start); Jul 12, 2013 at 14:04
  • @Quartermeister - That is good! I didn't know that. That should be a separate answer, and it's better than mine because it addresses it exactly as the OP asked it.
    – David
    Jul 12, 2013 at 14:13
6

You can have a conditional check in the ToString overload.

string _text = _start.ToString((_start.Hour > 9) ? "hh:mm tt" : " h:mm tt");
3
  • I think you might need a % 12 and possibly more logic in here - _start.Hour ranges from 0 to 23 but h and hh range from 1 to 12 twice
    – Rawling
    Jul 12, 2013 at 14:39
  • @Rawling - Sorry, I'm not quite understanding. Could you give a time where the above would break OP's requirements? The > 9 was to detect if there are double digits so I guess you could replace that with _start.Hour.ToString().Length > 1.
    – keyboardP
    Jul 12, 2013 at 14:59
  • 1
    Well, if it's 1 PM, _start.Hour will be 13 but hh will give 01.
    – Rawling
    Jul 12, 2013 at 16:33
3

If you want to do it all with a format string, you can use an alignment component. See Composite Formatting on MSDN.

Format Item Syntax

Each format item takes the following form and consists of the following components:

{index[,alignment][:formatString]}

...

Alignment Component

The optional alignment component is a signed integer indicating the preferred formatted field width. If the value of alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative. If padding is necessary, white space is used. The comma is required if alignment is specified.

You want to right-align the result in an eight-character field, so you would use an alignment component of positive 8 with a format string with a single h as in @David Stratton's answer, like this:

string _text = string.Format("{0,8:h:mm tt}", _start);
1
  • As David said, a better answer. Thank you.
    – Deverill
    Jul 12, 2013 at 21:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.