System.DateTime Custom Formatters

Problem

Recently a customer opened a ticket thinking that there is an issue with the DateTime formatting function. They had the following piece of code.

namespace DateTimeDefect
{
    class Test
    {
            static void Main(string[] args)
            {
                DateTime dt = DateTime.Now;

                Console.WriteLine(dt.ToString(“f”));
                Console.WriteLine(dt.ToString(“ff”));
                Console.WriteLine(dt.ToString(“fff”));

                Console.ReadLine();
            }
    }
}

Except for dt.ToString(“f”) the other two were returning correct results. Here’s the output…

Friday, May 30, 2014 12:53 AM <- Here’s the issue, customer expects a numeric value as shown below
53
534

Here’s what MSDN says about custom formatter ‘f’, ‘ff’, ‘fff’.

“f”: The tenths of a second in a date and time value. More information: The “f” Custom Format Specifier.

Sample:
6/15/2009 13:45:30.617 -> 6
6/15/2009 13:45:30.050 -> 0

“ff”: The hundredths of a second in a date and time value. More information: The “ff” Custom Format Specifier.

Sample:
6/15/2009 13:45:30.617 -> 61
6/15/2009 13:45:30.005 -> 00

“fff”: The milliseconds in a date and time value. More information: The “fff” Custom Format Specifier.

Sample:
6/15/2009 13:45:30.617 -> 617
6/15/2009 13:45:30.0005 –> 000

 
So what’s going wrong here?

The issue here is that dt.ToString(“f”) is not recognized as a custom format specifier. Please read below documentation from MSDN…

A custom date and time format string consists of two or more characters. Date and time formatting methods interpret any single-character string as a standard date and time format string. If they do not recognize the character as a valid format specifier, they throw a FormatException. For example, a format string that consists only of the specifier “h” is interpreted as a standard date and time format string. However, in this particular case, an exception is thrown because there is no “h” 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.

For example, “%h” is interpreted as a custom date and time format string that displays the hour represented by the current date and time value. You can also use the ” h” or “h ” format string, although this includes a space in the result string along with the hour. The following example illustrates these three format strings.

 
Resolution

So in this case the date time formatting method is interpreting this single character string as a standard date and time format string. To work around this, following are the options that you have…

1. Use dt.ToString(“%f”)
2. Use dt.ToString(“ f”);// add a space before ‘f’
3. Use dt.ToString(“f ”);// add a space after ‘f’

So if we change above code to…

static void Main(string[] args)
{
      DateTime dt = DateTime.Now;
      Console.WriteLine(dt.ToString(“%f”));
      Console.WriteLine(dt.ToString(“ff”));
      Console.WriteLine(dt.ToString(“fff”));
      Console.ReadLine();
}

Now program output looks as follows…

8
87
873

Based on last value output you can verify the first two. Issue resolved.

Appreciate your comments...