简体   繁体   English

字符串未被识别为有效的日期时间“格式 dd/MM/yyyy”

[英]String was not recognized as a valid DateTime " format dd/MM/yyyy"

I am trying to convert my string formatted value to date type with format dd/MM/yyyy .我正在尝试将我的字符串格式值转换为格式为dd/MM/yyyy的日期类型。

this.Text="22/11/2009";

DateTime date = DateTime.Parse(this.Text);

What is the problem?问题是什么? It has a second override which asks for IFormatProvider .它有第二个覆盖要求IFormatProvider What is this?这是什么? Do I need to pass this also?也需要通过这个吗? If Yes how to use it for this case?如果是,如何在这种情况下使用它?

Edit编辑

What are the differences between Parse and ParseExact ? ParseParseExact之间有什么区别?

Edit 2编辑 2

Both answers of Slaks and Sam are working for me, currently user is giving the input but this will be assured by me that they are valid by using maskTextbox. Slaks 和 Sam 的两个答案都对我有用,目前用户正在提供输入,但我将通过使用 maskTextbox 确保它们是有效的。

Which answer is better considering all aspects like type saftey, performance or something you feel like考虑到类型安全、性能或您喜欢的其他方面,哪个答案更好

Use DateTime.ParseExact .使用DateTime.ParseExact

this.Text="22/11/2009";

DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);

You need to call ParseExact , which parses a date that exactly matches a format that you supply.您需要调用ParseExact ,它会解析与您提供的格式完全匹配的日期。

For example:例如:

DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);

The IFormatProvider parameter specifies the culture to use to parse the date. IFormatProvider参数指定用于分析日期的区域性。
Unless your string comes from the user, you should pass CultureInfo.InvariantCulture .除非您的字符串来自用户,否则您应该传递CultureInfo.InvariantCulture
If the string does come from the user, you should pass CultureInfo.CurrentCulture , which will use the settings that the user specified in Regional Options in Control Panel.如果字符串确实来自用户,则应传递CultureInfo.CurrentCulture ,它将使用用户在控制面板的区域选项中指定的设置。

Parsing a string representation of a DateTime is a tricky thing because different cultures have different date formats.解析 DateTime 的字符串表示是一件棘手的事情,因为不同的文化有不同的日期格式。 .Net is aware of these date formats and pulls them from your current culture ( System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat ) when you call DateTime.Parse(this.Text) ; .Net 知道这些日期格式,并在您调用DateTime.Parse(this.Text)时从您当前的文化( System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat )中提取它们;

For example, the string "22/11/2009" does not match the ShortDatePattern for the United States (en-US) but it does match for France (fr-FR).例如,字符串“22/11/2009”与美国 (en-US) 的ShortDatePattern不匹配,但与法国 (fr-FR) 匹配。

Now, you can either call DateTime.ParseExact and pass in the exact format string that you're expecting, or you can pass in an appropriate culture to DateTime.Parse to parse the date.现在,您可以调用DateTime.ParseExact并传递您期望的确切格式字符串,或者您可以将适当的文化传递给DateTime.Parse以解析日期。

For example, this will parse your date correctly:例如,这将正确解析您的日期:

DateTime.Parse( "22/11/2009", CultureInfo.CreateSpecificCulture("fr-FR") );

Of course, you shouldn't just randomly pick France, but something appropriate to your needs.当然,你不应该只是随机选择法国,而是适合你需要的东西。

What you need to figure out is what System.Threading.Thread.CurrentThread.CurrentCulture is set to, and if/why it differs from what you expect.您需要弄清楚System.Threading.Thread.CurrentThread.CurrentCulture设置为什么,以及它是否/为什么与您的预期不同。

Although the above solutions are effective, you can also modify the webconfig file with the following...虽然上述解决方案都有效,但您也可以使用以下内容修改 webconfig 文件...

<configuration>
   <system.web>
     <globalization culture="en-GB"/>
   </system.web>
</configuration>

Ref : Datetime format different on local machine compared to production machine参考: 与生产机器相比,本地机器上的日期时间格式不同

You might need to specify the culture for that specific date format as in:您可能需要为该特定日期格式指定文化,如下所示:

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); //dd/MM/yyyy

    this.Text="22/11/2009";

    DateTime date = DateTime.Parse(this.Text);

For more details go here:有关更多详细信息,请访问此处:

http://msdn.microsoft.com/en-us/library/5hh873ya.aspx http://msdn.microsoft.com/en-us/library/5hh873ya.aspx

Based on this reference , the next approach worked for me:基于这个参考,下一个方法对我有用:

// e.g. format = "dd/MM/yyyy", dateString = "10/07/2017" 
var formatInfo = new DateTimeFormatInfo()
{
     ShortDatePattern = format
};
date = Convert.ToDateTime(dateString, formatInfo);

After spending lot of time I have solved the problem花了很多时间后,我解决了问题

 string strDate = PreocessDate(data);
 string[] dateString = strDate.Split('/');
 DateTime enter_date = Convert.ToDateTime(dateString[1]+"/"+dateString[0]+"/"+dateString[2]);
private DateTime ConvertToDateTime(string strDateTime)
{
DateTime dtFinaldate; string sDateTime;
try { dtFinaldate = Convert.ToDateTime(strDateTime); }
catch (Exception e)
{
string[] sDate = strDateTime.Split('/');
sDateTime = sDate[1] + '/' + sDate[0] + '/' + sDate[2];
dtFinaldate = Convert.ToDateTime(sDateTime);
}
return dtFinaldate;
}

使用它将字符串转换为日期时间:

Datetime DT = DateTime.ParseExact(STRDATE,"dd/MM/yyyy",System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat)

Just like someone above said you can send it as a string parameter but it must have this format: '20130121' for example and you can convert it to that format taking it directly from the control.就像上面有人说的那样,您可以将其作为字符串参数发送,但它必须具有以下格式:例如,“20130121”,您可以直接从控件中将其转换为该格式。 So you'll get it for example from a textbox like:因此,例如,您将从如下文本框中获取它:

date = datetextbox.text; // date is going to be something like: "2013-01-21 12:00:00am"

to convert it to: '20130121' you use:将其转换为:'20130121' 你使用:

date = date.Substring(6, 4) + date.Substring(3, 2) + date.Substring(0, 2);

so that SQL can convert it and put it into your database.以便SQL可以将其转换并放入您的数据库中。

Worked for me below code:以下代码为我工作:

DateTime date = DateTime.Parse(this.Text, CultureInfo.CreateSpecificCulture("fr-FR"));

Namespace命名空间

using System.Globalization;

You can use also你也可以使用

this.Text = "22112009";
DateTime newDateTime = new DateTime(Convert.ToInt32(this.Text.Substring(4, 4)), // Year
                                    Convert.ToInt32(this.Text.Substring(2,2)), // Month
                                    Convert.ToInt32(this.Text.Substring(0,2)));// Day

我还注意到,有时如果您的字符串在前面或结尾有空白空间或在 DateTime 值中附加任何其他垃圾字符,那么我们也会收到此错误消息

Change Manually :手动更改:

string s = date.Substring(3, 2) +"/" + date.Substring(0, 2) + "/" + date.Substring(6, 4);

From 11/22/2015 it will be converted in 22/11/2015从 2015 年 11 月 22 日起,它将在 2015 年 11 月 22 日转换

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析dd / MM / yyyy格式时,发生DateTime解析错误。 字符串未被识别为有效的日期时间 - DateTime parsing error while parsing dd/MM/yyyy format. String was not recognized as valid datetime “字符串未被识别为有效的日期时间。” 格式“yyyy-MM-dd”我如何使用这种格式 - "String was not recognized as a valid DateTime." format "yyyy-MM-dd" how i can use this format 对于字符串格式dd / MM / yyyyTHH:mm,“字符串未被识别为有效的DateTime” - “String was not recognized as a valid DateTime” for string format dd/MM/yyyyTHH:mm 将字符串格式转换为 mm/dd/yyyy 中的日期时间 - Converting string format to datetime in mm/dd/yyyy System.FormatException:字符串未被识别为有效的日期时间 - 尝试转换 MM/DD/YYYY 时 - System.FormatException: String was not recognized as a valid DateTime - when trying to convert MM/DD/YYYY C# 解析为 yyyy-MM-ddTHH:mm:ss 格式字符串未被识别为有效的日期时间 - C# Parse to the yyyy-MM-ddTHH:mm:ss format String was not recognized as a valid DateTime 字符串未被识别为格式为“MM/dd/yyy hh:mm:ss tt”的有效日期时间 - String was not recognized as a valid DateTime with the format of “MM/dd/yyy hh:mm:ss tt” 将dd / mm / yyyy中的日期时间字符串转换为日期时间格式mm / dd / yyyy - Convert date time string in dd/mm/yyyy to datetime format mm/dd/yyyy 当系统日期格式为dd / MM / yyyy时,将字符串的日期格式转换为datetime MM / dd / yyyy - Convert date format of string to datetime MM/dd/yyyy when system date formate is dd/MM/yyyy DateTime格式将mm / dd / yyyy转换为yyyy / mm / dd - DateTime format conversion mm/dd/yyyy to yyyy/mm/dd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM