Вопрос

I've tried with several different format strings but I can't get it to parse a date like:

date = "10/16/13";
DateTime endDate = DateTime.ParseExact(date, "M-dd-yy", CultureInfo.InvariantCulture);

What am I missing?!

Это было полезно?

Решение

For it to parse the date your format needs to be the same. Change "M-dd-yy" to "M/dd/yy" Assuming that the month is a single digit and the day is always 2 digits.

Другие советы

Here you go this should work just fine. You just need to be aware that it will set a default time of 12:00 am because you are not specifying the time in your string.

class Program 
{
    static void Main(string[] args)
    {
        string date = "10/16/13";

        //This is usually the safer way to go
        DateTime result;
        if(DateTime.TryParse(date, out result))
            Console.WriteLine(result);

        //I think this is what you were trying to accomplish
        DateTime result2 = Convert.ToDateTime(date, CultureInfo.InvariantCulture);

        Console.ReadKey();
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top