簡體   English   中英

無法將字符串識別為有效的DateTime。 ParseExact-剛剛的日期

[英]String was not recognized as a valid DateTime. ParseExact - Just Date

我嘗試了幾種不同的格式字符串,但無法解析如下日期:

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

我想念什么?!

為了解析日期,您的格式必須相同。 將“ M-dd-yy”更改為“ M / dd / yy”。假設月份是一個數字,並且日期始終是2個數字。

在這里,您應該可以正常運行。 您只需要知道它將設置默認時間12:00 am,因為您沒有在字符串中指定時間。

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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM