Take the 2-minute tour ×

I have a strange business requirement to output dates as 01:00 through 24:00, instead of the usual 00:00 through 23:00. It is really a crazy requirement, but unfortunately I don't think I can avoid it.

This will be a configuration option in our software, so I'll still need to support the normal 00-23 as well, so I'm hoping I can do this somehow with format strings. But I'm also considering using a regex to post-process the result string if that makes it easier.


share|improve this question
So in other words, you need to display 24:XX instead of 00:XX and nothing else changes, is that it? –  zneak Sep 2 '10 at 20:27
It needs to display 24:XX but the twist is it also needs to be shown from the previous date. E.g. 9/7/2010 00:00 should be displayed as 9/6/2010 24:00. –  randbrown Sep 9 '10 at 13:39
add comment

2 Answers

up vote 2 down vote accepted

This sounds like a good candidate for a wrapper class which is used to format the DateTime according to business rules:

public class BusinessDateTimeFormatter
    public BusinessDateTimeFormatter(DateTime dateTime)
        _dateTime = dateTime;

    public override string ToString()
        return String.Format("{0} {1}:{2}", _dateTime.Date, _dateTime.Hour + 1, _dateTime.Minute);

Alternatively, and perhaps even more correctly, you can create a type which represents the business's idea of a DateTime. In object-oriented design, this replacement of primitive types with custom types happens frequently, since the domain to be modeled has special restrictions just like this one that you describe.

share|improve this answer
A customer formatter for a date sounds like the way to go for me. You are encapsulating it and from the name its clear why its been done –  JonWillis Sep 2 '10 at 20:40
I ended up doing something along the lines of this suggestion - a simple business class to handle the formatting. I chose this approach because it's currently only required in one area of my application so it was this simplest solution for now. If it becomes required in other feature areas of the app, I may consider implementing a more robust custom formatter or something like that. –  randbrown Sep 9 '10 at 13:44
add comment

Extend .NET by creating your own format provider: http://msdn.microsoft.com/en-us/library/0asazeez(VS.71).aspx

share|improve this answer
I don't think this is a particularly good idea. The responsibility of IFormatProvider is to format based on a culture, not business rules. While it would work, it is warping the responsibility of that interface and the corresponding overload. Since this is a business concern, the logic should go in the model. –  codekaizen Sep 2 '10 at 20:35
@code - I disagree. He basically is creating a custom culture, not defining business logic (he's using standard DateTime behavior, just displaying the value differently to the user). This is exactly what IFormatProvider is for. –  Jon B Sep 2 '10 at 20:39
@Jon B - How is custom business logic a culture? He specifically states that this is a business requirement. –  codekaizen Sep 2 '10 at 20:40
@code business logic != business requirement. The business require is that dates be displayed in a specific culture. It could just as easily be EN-US. It happens to be a non-standard "culture" where times run from 1-24. –  Jon B Sep 2 '10 at 20:48
Having a format provider will also help a lot in conversions. For example converting from a string "24:03:00". –  user333306 Sep 2 '10 at 21:08
show 1 more comment

Your Answer


