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.
Related
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);
anyone know whats the best way to get date of birth im only able to find date and time and trying to parse it as a string wont update or delete from the database i get this error
String was not recognized as a valid DateTime.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: String was not recognized as a valid DateTime.
Source Error:
Line 96: int labID =
int.Parse(hdfID.Value.ToString()); Line 97: Line 98:
_strMessage(objLab.commitUpdate(labID, txt_patientidI.Text, txt_testCodeI.Text, txt_patientcodeI.Text, txt_ageI.Text,
txt_refrangeI.Text, txt_result1I.Text, txt_result2I.Text,
txt_resultDescI.Text, txt_sexI.Text, txt_testTypeI.Text,
txt_unitsI.Text, txt_abnormalI.Text, Line 99:
(DateTime.ParseExact(txt_dobI.Text,"yyyy/mm/dd",null))),"update");
Line 100: _subRebind();
what should i use for date of birth im using sql server c# .net want to parse that to string to show in my textbox thanks
Try using DateTime.ParseExact. instead of DateTime.Parse
this.Text="22/11/2009";
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);
DateTime is the structure you'll need to use.
Whenever you just want the date, you can use DateTime.Date to get just the date component.
Now, if you're parsing something in a format which is different to what one would generally expect, you can define the format the parser will look at by using the ParseExact function instead.
DateTime myDate = DateTime.ParseExact(txt_dobI.Text,"dd/MM/yyyy",null);
The list of the actual formats can be obtained from here http://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx
However in short
dd/MM/yyyy - British/EU/Lots of Places Format
MM/dd/yyyy - American Format
yyyy/MM/dd - Standard Format
I was working with wpf application I did this and it worked out for me.
DateTime DOB = DateTime.ParseExact(DOBText.Text.ToString(),
"dd/MM/yyyy", CultureInfo.InvariantCulture);
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 :)
DateTime frm_datestart = DateTime.Parse(dateStart.Text);
This line throws the error:
Exception Details:
System.FormatException: String was not
recognized as a valid DateTime.
Where the entered string is from Jquery-UI, examples:
09/29/2010
09/30/2010
Anyone know what the correct format should be? I'm suprised this isn't working :S
You can use an overloaded version of the DateTime.Parse() method which accepts a second DateTimeFormatInfo parameter.
System.Globalization.DateTimeFormatInfo dti = new System.Globalization.DateTimeFormatInfo();
dti.ShortDatePattern = "MM/dd/yyyy";
DateTime dt = DateTime.Parse(dateStart.Text, dti);
look for DateTime.ParseExact method.
val = dateStart.Text.ToString("yyyy-M-d HH:mm:ss");
Use DateTime.ParseExact to specify format like this: DateTime.Parse("dd/MM/yyyy", dateStart.Text, null)
The problem with DateTime.ParseExact() method suggested in previous answers is, it fails on some Cultures. So your application may fail to run correctly on certain Operating Systems.
If you are sure that dateStart.Text will always be in the same format (i.e. en-US), you may try passing appropriate CultureInfo as a second argument. For format "MM/dd/yyyy" use CultureInfo.InvariantCulture.