簡體   English   中英

使用DateTime.ParseExact C#無法將字符串識別為有效的DateTime

[英]String was not recognized as a valid DateTime using DateTime.ParseExact C#

以為我解決了這個問題,但我仍然遇到問題。 嘗試運行此代碼以在提示中解析用戶輸入的日期和時間,但是我嘗試使用DateTime.ParseExact我得到錯誤無效的格式。 樣本日期:2015年7月24日下午9:08:19 PDT Jul 26,2015 2:13:54 PDT

        string afterpromptvalue = Prompt.ShowDialog("Enter earliest Date and Time", "Unshipped Orders");
        string beforepromptvalue = Prompt.ShowDialog("Enter latest Date and Time", "Unshipped Orders");

        string format = "MMM d, yyyy h:m:s tt PDT";
        CultureInfo provider = CultureInfo.InvariantCulture;

        DateTime createdAfter = DateTime.ParseExact(afterpromptvalue, format, provider);
        DateTime createdBefore = DateTime.ParseExact(beforepromptvalue, format, provider);`enter code here`

這是提示框的代碼:

public static class Prompt
{
    public static string ShowDialog(string text, string caption)
    {
        Form prompt = new Form();
        prompt.Width = 500;
        prompt.Height = 150;
        prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
        prompt.Text = caption;
        prompt.StartPosition = FormStartPosition.CenterScreen;
        Label textLabel = new Label() { Left = 50, Top=20, Text=text };
        TextBox textBox = new TextBox() { Left = 50, Top=50, Width=400 };
        Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70, DialogResult = DialogResult.OK };
        confirmation.Click += (sender, e) => { prompt.Close(); };
        prompt.Controls.Add(textBox);
        prompt.Controls.Add(confirmation);
        prompt.Controls.Add(textLabel);
        prompt.AcceptButton = confirmation;

        return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
    }

您提供的輸入與預期不同,您輸入的分數為2位而不是1位。 由於您的預期時間格式為h:m:s,因此您無法提供9:08:19,但您需要輸入9:8:19。

請參閱正確的格式用法msdn link:

https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

考慮到你的兩個字符串,你只需要使用mm說明符而不是m說明符,因為你的單個數字分鍾有前導零

string format = "MMM d, yyyy h:mm:s tt PDT";

舉個例子;

string s = "Jul 24, 2015 9:08:19 PM PDT";
DateTime dt;
if(DateTime.TryParseExact(s, "MMM d, yyyy h:mm:s tt PDT", CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    // 24.07.2015 21:08:19
}

string s = "Jul 26, 2015 2:13:54 PM PDT";
DateTime dt;
if(DateTime.TryParseExact(s, "MMM d, yyyy h:mm:s tt PDT", CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    // 26.07.2015 14:13:54
}

試試這個:問題可能是你轉換為具有特定格式的日期時間但不提供與輸入相同:

dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
      format = "ddd dd MMM yyyy hh:mm:ss tt zzz";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

通過Console程序快速檢查您的代碼,它向我的PC顯示它正常工作:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Globalization;

    namespace DateTimeConvert
    {
        class Program
        {
            static void Main(string[] args)
            {

                var text1 = "Jul 24, 2015 9:08:19 PM PDT";
                var text2 = "Jul 26, 2015 2:13:54 PM PDT";

                string format = "MMM d, yyyy h:m:s tt PDT";

                var date1 = DateTime.ParseExact(text1, format, CultureInfo.InvariantCulture);
                Console.WriteLine(date1);

                var date2 = DateTime.ParseExact(text2, format, CultureInfo.InvariantCulture);
                Console.WriteLine(date2);

                Console.ReadLine();
            }
        }
    }

您是否可以運行上面的代碼並查看它是否仍然拋出相同的異常? 可能還有一些其他領域需要檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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