Im stuck here why i run my project in local properly but when i upload to server got this error.
i passing like this.
///////////////////in js///////////////////
'&dFrom='+Ext.getCmp('txtDateFrom').getValue().dateFormat('m/d/Y')
'dTo=' + Ext.getCmp('txtDateTo').getValue().dateFormat('m/d/Y')
///////////////////in c/////////////////////
DateTime dFrom;
DateTime dTo;
dFrom = Convert.ToDateTime(Request.Params["dFrom"]);
dTo = Convert.ToDateTime(Request.Params["dTo"]);
This is a culture problem
try to use ParseExact:
DateTime.ParseExact(Request.Params["dFrom"], "MM/dd/yyyy", CultureInfo.InvariantCulture)
Hope this helps.
Supply to convert culture(IFormatProvider).
http://msdn.microsoft.com/en-us/library/9xk1h71t.aspx
You might want to try using DateTime.Parse() instead of Convert.ToDateTime
DateTime dFrom = DateTime.Parse(Request.Params["dFrom"]);
DateTime dTo = DateTime.Parse(Request.Params["dTo"]);
You could try DateTime.Parse(string) and see if it gets a better result, otherwise the GET-string should be in another format. Also the CurrentCulture could modify the output.
I was having this same problem, it was working locally as dd-mm-yy but after publishing my site to the server I got error: "String was not recognized as a valid DateTime" so I changed it to mm-dd-yy and it worked on the server but then locally I was getting the error, I'm assuming because my asp.net host is in USA so the server date format is different from here in Australia. So to fix this annoying problem I simply changed the date setting in my control panel to mm-dd-yy and now no more DateTime errors :)
Related
I got this problem about Convert String to Datetime.
This is my code:
#if (Convert.ToDateTime(item.EXPIRED_DATE) <= DateTime.Now)
{
<td><span style="color:red;">#item.EXPIRED_DATE</span></td>
}
When I run it on my localhost, it worked. But when I publish it on Webserver, I got this Error Message:
Exception Details: System.FormatException: String was not recognized as a valid DateTime.
And this is item.EXPIRED_DATE value
"31/01/2018"
Please help me!
You could make use of DateTime.ParseExact:
DateTime.ParseExact(item.EXPIRED_DATE, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Why you got the error you have mentioned?
As you could read here Convert.ToDateTime, since the value you pass is not null, would invoke DateTime.Parse and return its result. The DateTime.Parse as it is mentioned here (look at the question: Which method do I call?) parses a date and time string by using the conventions of the current culture. So your program current's cutlure is not has not a format for DateTime as the one you want to use. This is why you have to use DateTime.ParseExact method providing the correct format.
I am converting one string to DateTime variable like this
DateTime selecteddatetest = Convert.ToDateTime("09/21/2017");
This works fine in my production Server, But when I run this code in my local development machine, this throws an error
System.FormatException: 'String was not recognized as a valid DateTime.'
Can anyone please point out what I am missing here?
You could use ParseExact if the time format is consistent:
DateTime.ParseExact("09/21/2017","MM/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture)
Its probably a localisation issue between the two machines, try specifying the date in the format "2017-09-21" and it should work everywhere.
You are likely using a different culture between the two machines.
For example, the server is using the US culture which expects the format MM/dd/yyyy so your parsing works.
You local machine may be using a culture such as UK which expects the format dd/MM/yyyy and as there is no month 21 it fails.
You can specify the culture explicitly if you know it's always going to be the same:
Convert.ToDateTime("09/21/2017", new System.Globalization.CultureInfo("en-US"));
It may also work with an invariant culture:
Convert.ToDateTime("09/21/2017", System.Globalization.CultureInfo.InvariantCulture);
You may also use ParseExact to specify the desired format:
DateTime.ParseExact("09/21/2017", "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
I am getting date as string parameter that is ExpDate as 12/16(month/year), i need to convert it as DateTime and save it to the sql db, i convert the above string to date as
paymentMethod.ExpirationDate = Convert.ToDateTime(ExpDate);
this absolutely working in my machine and saved in my db. but this is throws an error in my colleague's machine as
String was not recognized as a valid DateTime.
i have googled it and i come to know that i need to do cultureinfo. but i am getting the date as MM/yy only that is why i cant go through it. can anyone tell what do i need to do here?
You should use this overload:
https://msdn.microsoft.com/ru-ru/library/kc8s65zs(v=vs.110).aspx
paymentMethod.ExpirationDate = DateTime.Parse(ExpDate, CultureInfo.CurrentCulture);
I am trying to convert a given string which comes from a webservice to datetime. It works on local machine but when I try it from remote server, it gives an error like
"String was not recognized as a valid DateTime.".
Please see the code in below and recommend a general solution for this problem.
DateTime test = DateTime.ParseExact(XmlStringHelper.GetTagValue(result, "date"), "ddMMM", System.Globalization.CultureInfo.InvariantCulture);
Note that, value of the date parameter comes as "13FEB".
Thanks for your helps.
It seems the server is probably running with different culture as you local machine. The result from XmlStringHelper.GetTagValue(result, "date") is probably in an other formatting. Comapare the culture settings and the result of the XmlStringHelper.GetTagValue(result, "date") on both machines.
Your issue is that the date string from the web-service is localized.
Thus, you will have to adopt to the culture of the web-service, like:
DateTime test = DateTime.ParseExact(XmlStringHelper.GetTagValue(result, "date"), "ddMMM",System.Globalization.CultureInfo.*RemoteServerCulture*);
I know such questions are in ton in SO but my situation seems little weird to me.
I have a textbox with a calendar extender control on my aspx page
Default format is "d" in extenders date format property.
When I choose my date say 15th May 2012 from the calendar,it gives me 5/15/2012, which is fine.
Since its a string and my db field is oftype datetime, so I am using
Convert.ToDateTime(TextBox.Text); // TextBox.Text = 5/15/2012
But it throws the exception,
string was not recognized as valid datetime.
I then Change the code and used DateTime.Parse() but the issue remains. Then i tried to reformat the date something like this,
Convert.ToDateTime(string.Format("0:MM-dd-yyyy",TextBox.Text)).Date
but still its throwing exceptions..
Please help me.
Use the following,
DateTime dt = DateTime.ParseExact(TextBox.Text, "dd/MM/yyyy",
CultureInfo.InvariantCulture);
There's probably a difference between your system's DateTime format and the DateTiem format the extender uses.
I suppose that your dev machine date-time format is not equal to MM/DD/YYYY, but something else (for example DD/MM/YYYY). Have a look on your computer Regional settings to see your system date time format.