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);
Related
I´ve been trying to add some lines of a .csv document to a SQL Database ,but in a point i get this exception:
SYSTEM.FORMATEXCEPTION: STRING '0000-00-00 00:00:01' WAS NOT RECOGNIZED AS A VALID DATETIME.
AT SYSTEM.DATETIMEPARSE.PARSE(READONLYSPAN`1 S, DATETIMEFORMATINFO DTFI, DATETIMESTYLES STYLES)
AT SYSTEM.DATETIME.PARSE(STRING S)
AT FRAME2020.OBJETOSERVICIOS.DEVOLVERSERVICIOS(STRING[] LISTA) IN
D:\TRABAJO\FRAME2020\FRAME2020\CONEXIONBDD.CS:LINE
And i dont know how to solve it , i tried with try/except but it doesnt work with my approach ,any recommendations?
Problem is in your date which you try to set: 0000-00-00 00:00:01, there isn't day 0, month 0 and year 0.
Minimal DateTime is 1-1-0001 00:00:00
You haven't showed us any code, so I'm making some assumptions here. It looks like you're using DateTime.Parse to parse a column in your csv source that you expect to be a DateTime. However, your source has values in that column which do not parse into a valid DateTime. Such as 0000-00-00 00:00:01.
Others have already pointed out that 0000-00-00 00:00:01 is not a valid DateTime. In order to address this issue, you can use a different method to parse that column. For example, DateTime.TryParse. This will attempt to parse a string into a DateTime and return false if it fails or true if it succeeds.
string s = "0000-00-00 00:00:01";
if (!DateTime.TryParse(s, out DateTime dt))
{
// The string could not be parsed to a DateTime.
}
else
{
// The string was successfully parsed and the variable dt will have the parsed
// DateTime.
}
Then once you have that, you'll know if you successfully parsed the string into a DateTime and you can do something other than throw a FormatException when it fails.
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 have a webservice method that gets data from sql of the format
2012-11-18 11:21:03 when i save it to C# string it becomes this format: 18.11.2012 11:21:03
How do i change it back to the SQL format 2012-11-18 11:21:03 ?
Parse it into a dateTime again
DateTime myTime = DateTime.Parse(myString);
and back into a proper to string
myTime.ToString("yyyy-MM-dd HH:mm:ss");
Or just read it into a datetime and cut out the middleman.
You can get the universally sortable string format (which looks like the one used by SQL server) by using the format string "u" like this:
var dateTimeString = String.Format("{0:u}", yourDateTime);
Simply run the below code,
var newDateTime = oldDateTime.Date.ToString("yyyy-MM-dd HH:mm:ss");
Its just converting it back to the SQL Format DATETIME
Trouble with Dates as strings is they are ambiguous and the formats can vary based on where you are in the world, or even local machine settings. You might assume a date string is yyyy-mm-dd but what if it is actually yyyy-dd-mm? Some dates will appear to work and some will be invalid.
In other words is 2013-02-10 the 10th of February or is it the 2nd of October? If it is just a string you have no way of knowing for sure what was intended.
Your best bet as suggested by #Haedrian is to store in a DateTime C# object, not a string. That way it is never ambiguous and you have access to various date specific functions. If you must store as a string you can convert back to a date as above or use
DateTime.TryParse(datestring, out dateVariable);
which won't throw an exception for an invalid format. Depends if you want exceptions!
Also I would suggest if you must use strings to use a 3 character month in strings, which again eliminates the ambiguity, e.g.
"dd-MMM-yy hh:mm tt"
I want to accept date in dd-MM-yyyy format using textbox I have used ajax calendar also.
DateTime.ParseExact(txtDate.Text,"dd-MM-yyyy",null)
and
Convert.ToDateTime(txtDate.Text)
are both throwing exception:
string was not recognized as valid datetime.
I know when I will change my system dateformat which is currently MM-dd-yyyy to dd-MM-yyyy, it will start recognizing it but what is solution when I will publish it on server.
So is there any solution to parse it for current format ?
You need to pass an IFormatProvider parameter to the ParseExact method like :
CultureInfo provider = CultureInfo.GetCultureInfo("en-US")
DateTime.ParseExact(txtDate.Text,"dd-MM-yyyy",provider)
And don't forget to use the System.Globalization namespace
You need to use System.Threading.Thread.CurrentThread.CurrentCulture to get the user's current settings. Then use that to get the formatted date string.
DateTime now = DateTime.Now;
string formattedToCurrentUser = now.ToString(System.Threading.Thread.CurrentThread.CurrentCulture);
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 :)