The string was not recognized as a valid DateTime. There is an unknown word starting at index 0

前端 未结 5 1015
予麋鹿
予麋鹿 2021-01-19 10:53

I have the following C# that is giving me the error above when trying to parse string to datetime.

DateTime backupdate = System.Convert.ToDateTime(imageflowl         


        
5条回答
  •  轮回少年
    2021-01-19 11:03

    Yes - use "DateTime.ParseExact()" or "TryParseExact()" with a custom format string:

    http://msdn.microsoft.com/en-us/library/8kb3ffffd4.aspx

    DateTime currentdate;
    int result;
    try
    {
      // EXAMPLE: 2012-04-15 15:23:34:123 
      DateTime backupdate =
         DateTime.ParseExact (
           "yyyy-MM-dd HH:mm:ss:fff", //mind the casing
           imageflowlabel.Text, 
           CultureInfo.InvariantCulture);
      currentdate = System.DateTime.Now.AddHours(-2);    
      result = currentdate.CompareTo(backupdate);
    }
    catch (Exception ex)
    {
      ...
    

提交回复
热议问题