[英]String was not recognized as a valid DateTime using DateTime.ParseExact C#
Thought I solved this issue, but I am still having a problem with it. 以为我解决了这个问题,但我仍然遇到问题。 Trying to run this code to parse a Date and Time from user input in a prompt, but evertime I try to DateTime.ParseExact I get an error invalid format.
尝试运行此代码以在提示中解析用户输入的日期和时间,但是我尝试使用DateTime.ParseExact我得到错误无效的格式。 Sample Date : Jul 24, 2015 9:08:19 PM PDT Jul 26, 2015 2:13:54 PM PDT
样本日期: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`
Here's the code for the prompt box: 这是提示框的代码:
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 : "";
}
The input you gave is not the same as expected , you enter minutes with 2 digits instead of one. 您提供的输入与预期不同,您输入的分数为2位而不是1位。 Since your expected time format is h:m:s, you cannot provide 9:08:19, but you need to enter 9:8:19.
由于您的预期时间格式为h:m:s,因此您无法提供9:08:19,但您需要输入9:8:19。
See for correct format usage msdn link: 请参阅正确的格式用法msdn link:
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
Considering of your both string, you just need to use mm
specifier instead of m
specifier since your single digit minutes has leading zeros . 考虑到你的两个字符串,你只需要使用
mm
说明符而不是m
说明符,因为你的单个数字分钟有前导零 。
string format = "MMM d, yyyy h:mm:s tt PDT";
As an example; 举个例子;
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
}
and 和
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
}
try this: problem might be you are converting to date time with specific format but not giving the same as input: 试试这个:问题可能是你转换为具有特定格式的日期时间但不提供与输入相同:
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);
}
A quick check on your code via Console program and it reveals to my PC that it is working properly: 通过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();
}
}
}
Is it possible for you to run the above code and see if it still throws the same exception? 您是否可以运行上面的代码并查看它是否仍然抛出相同的异常? There might be some other areas that needs to be checked.
可能还有一些其他领域需要检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.