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*);
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 have a piece of code that crashes on my test server and not on my development server. I have 2 Windows 2012R2 Servers for dev and test. Both on same patch level, same .NET FrameWork version. Both have the same regional settings (Dutch) for current users and system local. This Dutch setting has the - as the date seperator. The following code snippet works on the dev server but crashes on the test server. I added the snippet with 2 lines of code in a console app and then I can reproducte the error.
The piece of code (simplified for demo console app) is:
string date = "28/02/2017";
DateTime dateDate = DateTime.ParseExact(date, "dd/MM/yyyy", null);
So this works on my dev server and crashed on test
I get this exception
Unhandled Exception: System.FormatException: String was not recognized as a vali
d DateTime.
at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInf
o dtfi, DateTimeStyles style)
at System.DateTime.ParseExact(String s, String format, IFormatProvider provid
er)
When I change the code to
string date = "28/02/2017";
DateTime dateDate = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
It works on both servers. When I output the CurrentCultue and CurrentUiCulture in the console app they are the same on both servers.
Any ideas what could go wrong on my test server? When I pass null in the ParseExact method I would expect that the code would also crash on my dev server because the CurrentCulture should then be used and then the date seperator / should cause an error.
Regards
Danny
null specifies that you want to use the current-culture instead, as opposed to passing CultureInfo.InvariantCulture.
MSDN:
If provider is null, the CultureInfo object that corresponds to the
current culture is used.
The / has a special meaning in parsing datetimes. They will be replaced with the currect culture's DateSeparator. (The "/" custom format specifier)
You could also mask them:
DateTime dateDate = DateTime.ParseExact(date, "dd'/'MM'/'yyyy", null);
The / characters is not interpreted literally in date format strings. It is get substituted with date separator according to the used format provider.
You need to enclose the / in single quotes to make it a literal character in the format string:
"dd'/'MM'/'yyyy"
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.
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 :)