views:

120

answers:

3

I have created a Web Service and deployed it on an internal server here. The server is set to UK datetime format.

The Web Service contains one method with the following signature:

public Result Scan(string value, string data, DateTime ScanDateTime)
{
}

When testing the webservice locally on the server, it will only accept values for the 3rd parameter in American datetime format. I need to work with UK date time format but I can't find a way to change this. All accounts and system locale are set to UK format.

A: 

Most probably from the web server if server is located in US

Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
saurabh
However, keep in mind that changing current thread's culture may break other code where implicit (default) current settings are used
abatishchev
+3  A: 

You can change the culture being used at runtime, however i'd strongly suggest holding a standardized format of yyyy-MM-dd since all cultures understand this format.

F.B. ten Kate
Correct you are, edited
F.B. ten Kate
+1  A: 

Because Web Service server-side code for input uses local culture settings which are different from en-GB, i.e. en-US.

But output data becomes formatted using client culture settings.

Input format should be documented by server maintainers and you should follow them.

Some DateTime conversion examples:

DateTime enGb = DateTime.ParseExact("02/09/2010", "dd/MM/yyyy", new CultureInfo("en-GB"));

string enUS_full = enGb.ToString(new CultureInfo("en-US")); // 9/2/2010 12:00:00 AM
string enUS_date = enGb.ToString("MM/dd/yyyy"); // 9/2/2010
string iso = enGb.ToString("yyyy-MM-dd"); // 2010-09-02

also you can use not hard-coded format but pre-configured:

string enUS = enGb.ToString(new CultureInfo("en-US").DateTimeFormat.ShortDatePattern); // 9/2/2010
string iso = enGb.ToString(CultureInfo.InstalledUICulture.DateTimeFormat.ShortDatePattern); // 2010-09-02
abatishchev
Are these local culture settings set anywhere, in a config file?
Mark 909
Client culture settings are taken from Windows settings, i.e. `System.Globalization.CultureInfo.CurrentCulture` is `new CultureInfo("en-GB")`. If you want to change the outgoing data format, do a convert. I'll update my post.
abatishchev
@Mark: See my updated post
abatishchev
I have full control over the server. it's the conversion of the input parameter that is the issue. I would've thought there'd be a way to configure the server / Webservice to accept the UK date format by default without having to resort to anything in code.
Mark 909
it's also worth mentioning that unless I change the signature for the datetime parameter to a string it won't even get as far as the code since it can't parse the UK date format into the required DateTime parameter
Mark 909
@Mark: I recommend to use ISO format on both sides because its a best-practice because you escape from the reason of the problem. Using one standardized, common datetime format `yyyy-MM-dd`
abatishchev