Friday, 15 May 2015

c# - Issue with datetime "String was not recognized as a valid DateTime" -


I am using the code below to check datetime and it is working fine in my machine, But once deployed, I am getting the

"The string was not recognized as a valid date time."

Please provide me the solution to work in all machines.

  DateTime1 = DateTime Pars ("16/05"); MM / DD string today's date = date time.Now Toaster ("MM / DD"); If convert (broken) (broken) (nowadays)> Convert. Tundetime (Date 1. Toastring ("MM / DD")) {// logic}  

Honestly, since both answers do not satisfy me, this is mine ..

Let's look at our code line through the line;

  DateTime Date = DateTime Pars ("16/05");  

uses your settings by default , if none IFormatProvider does not provide the second parameter as its overload This means that if you have any DD / MM from your current culture (or your current culture's means the current meaning then I would like to present the date of the current culture In separator ), this parsing operation will be successful. This means that this line throws probably FormatException which relies on existing culture settings.

  string nowadays date = date time.Now Toasting ("MM / dd");  

Datetime.now gives a local working time. With this, ToString () method you try to get it with a string representing the format MM / dd . but wait! You used the / format specifier again and still, you have not used any IFormatProvider . Since this format varies with the current culture date separator from the specifier, so your todaydate can be 05/16 , 05-16 or 05.16 . It relies entirely on the date separator that your current culture is used to.

  Convert. Tutorial (nowadays)  

Convert.ToDateTime method explicitly uses DateTime.Parse , because you have any Also, the IFormatProvider is not provided, it will use your current culture and this is the standard date and time format. As I said, the result may be nowadays can be 05/16 , 05-16 or 05.16 But it's no guarantee that your existing culture parses this string successfully because it can not be in in standard date and time formats MM / dd If it parses "16/05" successfully, it means there is a dd / MM format, in that case, of course can not be MM / dd as a standard date and time format. A culture no can parse DD / MM and MM / DD formats at the same time. In such a case, it does not know that the 01/02 string should be parsed as 2 January or 1 February, okay?

  Convert.ToDateTime (date1.ToString ("MM / DD"))  

Here is the same as todaydate It is a string, it will result in "05/16" result (this depends on the existing culture date separator) and still there is no guarantee of successfully parsing it. / P>

And as stated in the comments, there is no point in parsing your string in datetime, and it also gets the representation of the same string.

I strongly suspect that you try to compare your current date or not, compared to your parsed date time, you can use it to compare it with. This property gets as a datetime in the form of current date part plus midnight time. for example;

  dated DT; If (DateTime.TodayTypeExact ("16/05", "DD / MM", Culture Info. InvoiceTechTech, Datetime Style No, Out DT)) {if (Datetime.DateTime.DT) {// Your Operation}} < / Code> 

No comments:

Post a Comment