What is an elegant way to pull out common formats (e.g. datetime) for string.format into accessible constants?
Ideally I would like to do something like the following, but I get the below error when I try to use this code.
var now = DateTime.Now;
var format = "yyyy-MM-dd";
Console.WriteLine(string.Format("The date is {1:{0}}", format, now));
[System.FormatException: Input string was not in a correct format.] at Program.Main(): line 9
The reasoning behind this is that certain API's require a specific datetime format. I would like to be able to reference a single place to get that format, such that all or none of the calls will work.
I realize that the following will work, but it doesn't seem very elegant.
Console.WriteLine(string.Format("The date is {1:" + format + "}", format, now));