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);
Related
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 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*);
Convert.ToDateTime works in console app,
string a = "18/02/2015";
DateTime aa = Convert.ToDateTime(a);
Errors out in asp.net forms saying string is not righ format,
DateTime aa = Convert.ToDateTime(myTextBox.Text);
It used to work before, but since I got a newly installed server it started giving me this error..
Input string seem same to me "18/02/2015"
Convert.ToDateTime uses DateTime.Parse internally, with the current culture of server. And, the problem is your new server's current culture's DateTime format is different from your string.
You can use DateTime.ParseExact() instead of this.
Converts the specified string representation of a date and time to its
DateTime equivalent using the specified format and culture-specific
format information. The format of the string representation must match
the specified format exactly.
DateTime.ParseExact(myTextBox.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
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.
string strdate="15/06/2010";
DateTime dt =
DateTime.Parse(strdate,
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat);
i cannot able to get the datetime value as dd/mm/yyyy.
it is giving exception 'string is not recognized as a valid datetime'
oly if it is in 06/15/2010 it is working. how to get the same format in dt.
Well, presumably your thread's current culture expects MM/dd/yyyy. If you want to use dd/MM/yyyy then you should specify that explicitly. Personally I prefer ParseExact instead of Parse, as that gives more control. I would use something like:
DateTime dt = DateTime.ParseExact(strdate, "dd/MM/yyyy",
CultureInfo.InvariantCulture);
Note that if this is user input, you may want to use TryParseExact instead.
You're currently using the current culture, which seems to be set up for US style date formats. Try this instead:
DateTime.Parse(strdate, System.Globalization.CultureInfo.CreateSpecificCulture("en-GB"));
This tells the function to use UK date style format which works. You might want to change the en-GB to whatever culture your dates will be in. If you have many calls where the culture is important it might also be worth to set it for the whole thread rather than call by call.