简体   繁体   中英

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:

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.
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.

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:

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:

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:

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

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.

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"

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.

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.

I fixed it by using

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

See how simple it is.

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