I have got a very simple function that takes anything in as an object, but generally a string, and attempts to format it in a specific date format that we use for reports at work.
The function is generally called using the OnRowDataBound event of a GridView and returns the date as a string if it could parse it or the original input as a string.
Here is the function:
public string FormatDate(object input)
{
DateTime output;
return DateTime.TryParse(input.ToString(), out output)
? output.ToString("dd-MMM") : input.ToString();
}
Maybe I am being picky but the issue I have is the creation of a variable, in this case a DateTime object called output, which is created purely for the output of the TryParse function which I then convert to string in the format I specify.
Is there a better, more concise method of doing this?