简体   繁体   中英

C# String was not recognized as a valid DateTime

Error being thrown when the dateString is the following but worked earlier for a different time, not sure why it isn't working now.

 string dateString = "Jul 24, 2015 4:03:51 PM PDT";
            string format = "MMM dd, yyyy h:mm:ss tt PDT";
            CultureInfo provider = CultureInfo.InvariantCulture;
            DateTime time = DateTime.ParseExact(dateString, format, provider);
            Console.WriteLine(time);

Edited Code: The error is thrown either of the last two lines, sometimes the first DateTime will execute but not the second. The prompt window just asks for, first, the earliest date and time which is: Jul 24, 2015 6:26:15 AM PDT. And then another prompt for the latest DateTime which is: Jul 24, 2015 4:03:51 PM 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 dd, yyyy h:mm:ss tt PDT";
            CultureInfo provider = CultureInfo.InvariantCulture;

            DateTime createdAfter = DateTime.ParseExact(afterpromptvalue, format, provider);
            DateTime createdBefore = DateTime.ParseExact(beforepromptvalue, format, provider);

Edited again: I wanted to put the prompt dialog box code, because this may be the issue.

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 : "";
    }
}

Your code worked at my machine without any error. Try executing it on some other machine or in different solution. If it works means your solution need to be clean and build. If do not work means you you probably missing required references -

using System;
using System.Globalization;

A common error with date parsing is using dd instead of d . With dd , a value of 24 will pass, but 9 will not; the latter would have to be 09 instead. If you use a single d , however, then 9 , 09 , and 24 would all be allowed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM