Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a date in C# by example?

C# provides a lot of flexibility when formatting a DateTime object for a string representation, however, one has to know all format strings to use that flexibility.

If you want to display a date in the form "Fri, June 24", you can do it like this:

DateTime someDate = DateTime.Now;
Console.Write(someDate.ToString("ddd, MMMM dd"));

While this works well, it's hard for more sophisticated formats, especially for a developer working with it for the first time.

I want to achieve the same results returned from the code above, but with this:

DateTime someDate = DateTime.Now;
Console.WriteLine(someDate.ToString("Wed, June 12"));

The date specified as a string could be arbitrary. Essentially the format has to be determined by first parsing the date somehow. I know this approach has limitations (localization is one), but for simple scenarios it is much more understandable. Is there some way to do it apart from implementing it myself? I'm willing to use third-party libraries.

like image 718
Slavo Avatar asked Jul 06 '11 15:07

Slavo


3 Answers

Usually I just figure out what the correct format string should be (in your example "ddd, MMMM dd") and then store that as a constant somewhere in the application...

public static class DateTimeFormats
{
    public string DayOfWeekMonthDay = "ddd, MMMM dd";
}

then you can just refer to it

DateTime.Now.ToString(DateTimeFormats.DayOfWeekMonthDay)
like image 149
Jon Erickson Avatar answered Oct 21 '22 21:10

Jon Erickson


I can see how this may appear easier for the newer developer to work with, but there are a few issues to contend with in building a "date format from date string" function. I can imagine scenarios where such a function might fail, saying things like:

  • You said "May"; does that mean dates in June should format as "Jun" or "June"?
  • You said "June 12"; does that mean June 5th should format as "June 5" or "June 05"?

Now, you could explain to new developers that they need to be careful not to use sample date-format templates that are ambiguous. But this would require them to already understand how it could be ambiguous. They would need to already be thinking like the formatting function thinks.

This is why the date format strings are defined as they are - to be as specific as possible about the desired output format the developer wants/needs to produce. They prevent such ambiguities to the greatest extent possible.

If the developer will eventually need to "think like the formatting function" to get what they want, it's probably worth the time to learn the existing definitions.

like image 41
J Bryan Price Avatar answered Oct 21 '22 20:10

J Bryan Price


The strict answer to this question:

Is there some way to do it apart from implementing it myself? I'm willing to use third-party libraries.

Is no, unless a third party has done this already, you'll need to implement your own format string parser.

I share the opinion of most respondents that the effort required to do that is utterly out of proportion to the alternative of simply memorizing the DateTime formats already provided (or referring to their documentation). But, if you did undertake such effort, you would want to implement an ICustomFormatter, and an IFormatProvider that would provide it when requested.

See the ICustomFormatter documentation linked above for an example, but your task will involve providing a Format(string format, object arg, IFormatProvider formatProvider) method that takes a string in the format you are interested in and uses it to turn the DateTime passed in arg into a string matching that pattern.

Once this is done, and you have an IFormatProvider whose GetFormat() method returns your custom formatter, your sample code would look like this:

DateTime someDate = DateTime.Now;
Console.WriteLine(someDate.ToString("Wed, June 12", new CustomDateFormatter()));
like image 2
Dan J Avatar answered Oct 21 '22 22:10

Dan J