TxtPolCreatedDate.Text = Convert.ToDateTime(TxtPolCreatedDate.Text).ToString("dd/MMM/yyyy");
Its working fine on local machine but not working on server . I have also checked for the Date.Parse() and specify culture as suggested on
https://msdn.microsoft.com/en-us/library/9xk1h71t(v=vs.110).aspx.
But not working..
Try this:
DateTime.ParseExact(TxtPolCreatedDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Because Convert.ToDateTime uses the current culture of the server and probably the server's current culture's DateTime format is different from yours. So the solution is using ParseExact method and pass the culture explicitly.
Reference : DateTime.ParseExact Method (String, String, IFormatProvider)
Thanks a lot for your response.. But this is not working in my case.
I got solution.. If we add
[<globalization uiCulture="en" culture="en-GB" enableClientBasedCulture="true "/>]
in web.config file . then it will not give format exception..
Related
I have written a C# console application that after all of it's processing outputs a DateTime to disk. It does this like so:
writer.WriteLine(myDateTime).
This same console appication has no problems with using the following to read this DateTime back:
DateTime.Parse(reader.ReadLine())
However, upon attempting to use the following code in my separate Asp.Net program I recieve an error saying that my string is not a valid DateTime which is odd to say the least.
StreamReader reader = new StreamReader(#"D:\InformerReports\Archive\ReliabilityData\StartTime.hist");
string dateString = reader.ReadLine();
return DateTime.Parse(dateString);
I have checked and the string it is reading in is 10/25/2016 12:00:00 AM.
I have also attempted to use return DateTime.ParseExact(dateString, "dd/MM/yyyy hh:mm:ss tt",null) but this returns the same error.
I can't seem to fathom why identical code performed on the same file works in one case and not the other. I'd appreciate some help.
I guess the culture of the server is different from the machine you are testing from.
The correct format you have to use seems to be:
return DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt", null)
// 10/25/2016 12:00:00 AM
As an add-on to the previous answers.
To avoid the differing cultures across clients you can set the site culture in the global.asax files Application_BeginRequest method like below:
Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");
Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR");
This will force the above specified culture for each user accessing the site.
I am not sure if there are drawbacks to doing it this way, but this solved my issue in the past.
In the below code i have a date format which works on machine with date format day/month/year not in month/day/year.Pls help me to rectify the issue.
string startdate =DateTime.Parse(txtFromDate.Text + " 00:00:00").ToString("yyyy-MM-dd" + " 00:00:00");
You can use DateTime.ParseExact() to parse the date in specific format. See this MSDN article on how to use it.
In your case you can use something like this:
CultureInfo provider = CultureInfo.InvariantCulture;
string startdate = DateTime.ParseExact(txtFromDate.Text, "MM/dd/yyyy", provider).ToString("yyyy-MM-dd HH:mm:ss");
add this in your web config
<globalization culture="en-GB" uiCulture="en-GB" />
in teg
<system.web>
Depending on your requirements you can approach this question a couple different ways. Here are three off the top of my head:
1) Use the DateTime.ParseExact method.
2) Change the Regional settings in your operating system.
3) Change the culture configuration in your web.config.
I am inserting some data into a SharePoint list (via web services) and on my local machine I set a date field like this (hard coded in this example)
<Field Name='TimeOnScene'>" + DateTime.Parse("13/12/2011 1:00").ToString("yyyy-MM-ddTHH:mm:ssZ") + "</Field>
and it works fine on my local machine, but if I publish it to our web host and run the exact same code I get
{"Message":"String was not recognized as a valid DateTime.","StackTrace":"
//
//
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)\r\n at System.DateTime.Parse(String s)\r\n "ExceptionType":"System.FormatException"}
How is this possible?
Thanks in advance.
Edit:
we also moved from host to another two weeks ago and never had this issue before.
Use DateTime.ParseExact instead of Parse, the converting will be like the following code.
CurDate = DateTime.ParseExact(YourDateString, "dd/MM/yyyy hh:mm", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None)
The host could have different local in set on there machine. in the documentation
The string s is parsed using formatting information in the current DateTimeFormatInfo object, which is supplied implicitly by the current thread culture.
I have an application that works without any problem in a spanish server.
When i uploaded the application into the online server (an english windows), im getting exceptions (of type "input string is not a valid Datetime/Int32") with Convert.ToDateTime and Convert.ToInt32. Are any web.config line that could help me in this matter? I tried adding a globalization element with the Spanish culture, but didnt worked.
Could you give me a hand?
Thanks in advance.
Josema.
You need:
System.Globalization.CultureInfo culture =
new System.Globalization.CultureInfo("es-ES");
DateTime myDateTime = Convert.ToDateTime(string, culture);
Are you specifying a CultureInfo argument, as an IFormatProvider in your String.Format() calls?
You might have set uiculture instead of culture in the globalization element, see: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx.
...
<globalization culture="es-MX" />
...
You can also try using a more specific culture (like the one above es - mexico).
Ps. I have a site working like that (actually with culture="en" as in my case I needed to force english as my development computer was configured with spanish at the time).
I've written a asp.net app and one of my postback routines simply saves user submitted form data to a sql 2005 db. All runs great on my development machine but when I deploy to the live site I'm getting invalid dates from my parse date checker.
Basically it is expecting an american date format on the live machine but this is not what I want. The user needs to be able to enter in dd/MM/yyyy format. So a valid date like 21/10/2009 returns errors on live server but not on my dev machine. Below is the code that throws the exception.
DateTime dt;
dt = DateTime.Parse(sdate);
//sdate in GB dd/MM/yyyy format
Is it possible to force the parse routine to expect the date in dd/MM/yyyy format?
Do like this:
System.Globalization.CultureInfo cultureinfo =
new System.Globalization.CultureInfo("en-gb");
DateTime dt = DateTime.Parse("13/12/2009", cultureinfo);
You can use DateTime.ParseExact to specify an expected format.
Another option is to specify the culture you wish to use in the web.config file:
<system.web>
...
<globalization
culture="da-DK"
uiCulture="da-DK" requestEncoding="utf-8" responseEncoding="utf-8"
/>
</system.web>
Yes, ParseExact will do that as mentioned by Matt.
The code would be something like:
dt = DateTime.ParseExact(sdate, "dd/MM/yyyy", CultureInfo.InvariantCulture);