DateTime format Issue using Asp.net C# - c#

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.

Related

Convert.ToDateTime() not working on Server.. Give format exception

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..

ObjectDataSource fails to parse string to DateTime

I have a textbox with value that stores ValidFrom form value:
31.01.2012
and cultures set to:
<globalization culture="en-GB" uiCulture="en-GB"/>
in web.config.
And now, ObjectDataSource update method:
public static void UpdateLac(int id, DateTime ValidFrom)
{
/// ...
}
fails as I get exception that string cannot be parsed. However date in format dd.mm.yyyy (31.01.2012) is valid en-GB format and can be parsed (as far as I know). I have tested it with following code:
DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat;
var date = DateTime.Parse("31.01.2012", dtfi);
Console.Write(date.ToLongDateString());
So how come that ObjectDataSource internal conversion fails to convert string (31.01.2012) to DateTime in this example?
As far as I know the culture info is loaded directly from the OS ( in this case windows), you can check on your regional setting for the format specified. This is a screenshot from my pc:
http://imageshack.us/photo/my-images/96/engbg.png/
As you can see the format for short date is: dd/MM/aa, so maybe there is something going on with your server regional settings or the input should be: 31/01/12 instead of 31.01.2012
Hope this helps.

DateTime parsed differently in different environments?

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.

Problem with Convert.ToDateTime in asp.net

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).

c# and Date Culture Problems

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);

Categories