Question

I have an integer that will have the value of a year month and day. For example 20110504.

I am using TryPareseExact to format it into yyyy-MM-dd but it is not working.

Here is my function

public DateTime DateDisplay(int date)
{
    DateTime dateValue;

    if (DateTime.TryParseExact(date.ToString(), "yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dateValue))
        return dateValue;
    else
        return DateTime.MinValue;
}

It always go to the else and returns DateTime.MinValue. I want the date to be look like 2011/05/04. Would you be able to help me to identify where is my mistake?

Was it helpful?

Solution

I believe the format string should be "yyyyMMdd" if your input string has no hyphens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top