简体   繁体   English

如何在没有得到“字符串未被识别为有效的日期时间”的情况下转换日期。 - 错误?

[英]How to convert dates without getting 'String was not recognized as a valid DateTime.'-error?

I'm trying to convert dates for example 30/12/2000 to 2000-12-30 using this code:我正在尝试使用以下代码将日期转换为例如 30/12/2000 到 2000-12-30:

System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US");
DateTime.ParseExact(row.Cells[6].ToString(), "yyyy-MM-dd", enUS);

But I'm getting this error:但我收到了这个错误:

String was not recognized as a valid DateTime. 

Can someone help me please, Thank you in advance.有人可以帮助我吗,在此先感谢您。

You could use DateTime.TryParse() function and check if result is true or false.您可以使用DateTime.TryParse() function 并检查结果是真还是假。
So you could try to parse date with a specified format and, if its not right, try another one and so on...因此,您可以尝试使用指定格式解析日期,如果不正确,请尝试另一种格式,依此类推...

The reason why you are getting this is exactly as the exception says, the string is in an incorrect format, the reason is most likely that the machine doing the comparison has a different date time setting to yyyy-MM-dd.你得到这个的原因与异常所说的完全一样,字符串格式不正确,原因很可能是进行比较的机器与 yyyy-MM-dd 的日期时间设置不同。

If you are retrieving this from a database and the return value is of date time (or if you know that row.Cells[6] is a DateTime Field, the following should work:如果您从数据库中检索它并且返回值是日期时间(或者如果您知道 row.Cells[6] 是 DateTime 字段,则以下内容应该有效:

System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US");

if (row.Cells[6] != null)
    DateTime.ParseExact(((DateTime)row.Cells[6]).ToString("yyyy-MM-dd"), "yyyy-MM-dd", enUS);

The question is however why would you want to change the format, to display it on a form if so then you can just display it as follows:但是,问题是您为什么要更改格式,将其显示在表单上,如果是这样,则可以按如下方式显示:

if (row.Cells[6] != null)
    TextBox1.Text = ((DateTime)row.Cells[6]).ToString("yyyy-MM-dd");

EDIT Given that row.Cells[6] is a string, you will always have to know what the format of the string is, and if you do then it would be as simple as:编辑鉴于 row.Cells[6] 是一个字符串,您将始终必须知道字符串的格式是什么,如果您这样做,那么它会很简单:

With time:随着时间的推移:

DateTime ParsedDate = DateTime.ParseExact(row.Cells[6].ToString(), "dd-MM-yyyy h:mm", enUS);

Removing time and then parsing:删除时间然后解析:

DateTime ParsedDate = DateTime.ParseExact(row.Cells[6].ToString().Substring(0,10), "dd/MM/yyyy", enUS);

and then to output it in the format you want would be as simple as:然后到 output 它以您想要的格式简单如下:

TextBox1.Text = ParsedDate.ToString("yyyy-MM-dd");

You are specifying a en-US CultureInfo object, but 30/12/2000 is not a correct US date format您指定的是 en-US CultureInfo object,但 30/12/2000 不是正确的美国日期格式

Try this may resolve your issue试试这个可能会解决您的问题

String oldScheduledDate = "16-05-2011";
 DateFormat oldFormatter = new SimpleDateFormat("dd/MM/yyyy");
 DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
 try
 {
    Date oldDate = (Date)oldFormatter .parse(oldScheduledDate);
 }
 Catch(Exception ex ) { /// process exception}
System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US");
try {
 DateTime.ParseExact(row.Cells[6].ToString(), "yyyy-MM-dd", enUS);
}
catch (FormatException) {
 ...your code instead of error...
}

I believe that the problem is your second parameter.我相信问题出在你的第二个参数上。 If you are passing 30/12/200 as your input, it will fail because the second parameter is expecting it separated with dashes.如果您将 30/12/200 作为输入传递,它将失败,因为第二个参数期望它用破折号分隔。

use利用

  System.Globalization.CultureInfo.InvariantCulture

 DateTime.ParseExact(((DateTime)row.Cells[6]).ToString("yyyy-MM-dd"), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture)

; ;

you forgot the hours / minutes:你忘记了小时/分钟:

from the DB it comes like that (converted to string) "12/05/2011 0:00"从数据库中它是这样的(转换为字符串)“12/05/2011 0:00”

DateTime.ParseExact(theCellValue, "MM/dd/yyyy HH:mm", enUS).ToString("yyyy-MM-dd")

creates the string you want, but its totally stupid.创建你想要的字符串,但它完全愚蠢。 you should just make sure that the datetime gets in the first place formatted as you want.您应该确保首先按照您的需要格式化日期时间。 (then you never have to parse it) (那么你永远不必解析它)

If your source string is in dd/MM/yyyy, then you shouldn't be using US culture (MM/dd/yyyy) to parse the string.如果您的源字符串在 dd/MM/yyyy 中,那么您不应该使用美国文化 (MM/dd/yyyy) 来解析字符串。

System.Globalization.CultureInfo enGB = new System.Globalization.CultureInfo("en-GB");
DateTime temp = DateTime.ParseExact(row.Cells[6].Text, "dd/MM/yyyy", enGB);

row.Cells[6] = temp.ToString("yyyy-MM-dd");

Put this in the RowDataBound event of your grid.把它放在网格的 RowDataBound 事件中。

I fixed it by using我通过使用修复它

Convert.ToDateTime(row.Cells[6].Text)

See how simple it is.看看它有多简单。

暂无
暂无

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

相关问题 获取错误“字符串未被识别为有效的DateTime”。 - Getting error “String was not recognized as a valid DateTime.” 对于某些确切的日期,发生了“字符串未被识别为有效的DateTime。”错误 - "String was not recognized as a valid DateTime.” error occurs for some exact dates 始终获取字符串未被识别为有效的日期时间。 如何转换为当前格式 - always getting string was not recognized as valid datetime. how to convert for current format 错误消息为“字符串未被识别为有效的DateTime。” - Error Message as “String was not recognized as a valid DateTime.” 错误:字符串未被识别为有效的DateTime。 - Error: String was not recognized as a valid DateTime. 迄今为止的字符串解析错误“字符串未被识别为有效的DateTime。” - String to date parsing error “String was not recognized as a valid DateTime.” “字符串未被识别为有效的DateTime。”Window 7计算机环境中出现错误 - “String was not recognized as a valid DateTime.” Error occur in Window 7 computer environment 无法将字符串识别为有效的DateTime。 :仅服务器错误 - String was not recognized as a valid DateTime. : Error only in Server 错误“字符串未被识别为有效的日期时间。” 在 c# - error “String was not recognized as a valid DateTime.” in c# 字符串日期未被识别为有效的日期时间。 - String date was not recognized as a valid DateTime.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM