CultureInfo and DateTime.parse
-
Hello all, I have a jcaps web service I need to call with details of a document - this includes DateTime field which expects a Date in the format dd-MM-yyyy but I can't get my datetime to serialize in this format instead it always serializes in the format dd/MM/yyyy. This causes the web service to throw a SOAP exception. I've tried using a new CultureInfo which is a clone of the current CultureInfo and have changed the DateTimeFormat.DateSeparator = "-" - this will survive a ToString() when I provide the new CultureInfo but when I try a DateTime.Parse() providing the string and culture info it again uses the "/" date separator. I've also tried with ParseExact() to no avail. Is there a way to persist CultureInfo through serialization? Or (as I'm beginning to suspect) am I barking up the wrong tree? Thanks Paul ETA - Sorry should make clear the above has been attempted to see what the cultureinfo will persist across, when accessing the web service we just provide the datetime property.
-
Hello all, I have a jcaps web service I need to call with details of a document - this includes DateTime field which expects a Date in the format dd-MM-yyyy but I can't get my datetime to serialize in this format instead it always serializes in the format dd/MM/yyyy. This causes the web service to throw a SOAP exception. I've tried using a new CultureInfo which is a clone of the current CultureInfo and have changed the DateTimeFormat.DateSeparator = "-" - this will survive a ToString() when I provide the new CultureInfo but when I try a DateTime.Parse() providing the string and culture info it again uses the "/" date separator. I've also tried with ParseExact() to no avail. Is there a way to persist CultureInfo through serialization? Or (as I'm beginning to suspect) am I barking up the wrong tree? Thanks Paul ETA - Sorry should make clear the above has been attempted to see what the cultureinfo will persist across, when accessing the web service we just provide the datetime property.
Provided you call ParseExact with the appropriate parameters it should work:
string date = "02-11-1976"; DateTime dt = DateTime.ParseExact(date, "MM-dd-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
Parses it fine for me: What are you doing that is different?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
-
Provided you call ParseExact with the appropriate parameters it should work:
string date = "02-11-1976"; DateTime dt = DateTime.ParseExact(date, "MM-dd-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
Parses it fine for me: What are you doing that is different?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
I don't have any issue with the parsing as such it's when I do something with date time afterwards like .ToString() or when it's serialised prior to a web service call. In these cases the "-" is replaced by "/" causing (in my case) the webservice call to fail. I've re-tried this morning with a fresh head, and used your example but still have the issue. Cheers Paul
-
I don't have any issue with the parsing as such it's when I do something with date time afterwards like .ToString() or when it's serialised prior to a web service call. In these cases the "-" is replaced by "/" causing (in my case) the webservice call to fail. I've re-tried this morning with a fresh head, and used your example but still have the issue. Cheers Paul
Ah! Easy one: ToString always formats to the current Culture, unless you tell it otherwise. Try
string date = myDateTime.ToString("dd-MM-yyyy");
You will find a complete list of how to format a DateTime to a string here[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
-
Ah! Easy one: ToString always formats to the current Culture, unless you tell it otherwise. Try
string date = myDateTime.ToString("dd-MM-yyyy");
You will find a complete list of how to format a DateTime to a string here[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
Right thanks for that I was sort of assuming that would be the case - whilst I'm aware of the date formatting options I'm assuming that no such options exist for assigning date times to an object being passed to web service? Thanks for your responses, btw. Cheers Paul
-
Right thanks for that I was sort of assuming that would be the case - whilst I'm aware of the date formatting options I'm assuming that no such options exist for assigning date times to an object being passed to web service? Thanks for your responses, btw. Cheers Paul
Unless you are passing it as a string, then no options are needed: a DateTime is not as localised object, it is a number of microseconds since a reference date and time. It only becomes localised into any readable format when you extract bits of information, or change it to some other type. What are you passing to your web service?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
-
Unless you are passing it as a string, then no options are needed: a DateTime is not as localised object, it is a number of microseconds since a reference date and time. It only becomes localised into any readable format when you extract bits of information, or change it to some other type. What are you passing to your web service?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
We instantiate a document object which is a proxy class built automatically when creating the web reference, this contains a number of datetime properties. We populate the various properties including the datetime fields and call the service which accepts this Document object. When using Fiddler we can see the SOAP call and the datetime fields use the "/" date separator but the JCAPS service wants a "-" separator to consider it a valid Date and so fails. I'm assuming this is done as part of the serialization process when building the SOAP body and was hoping there was a way we could influence it.
-
We instantiate a document object which is a proxy class built automatically when creating the web reference, this contains a number of datetime properties. We populate the various properties including the datetime fields and call the service which accepts this Document object. When using Fiddler we can see the SOAP call and the datetime fields use the "/" date separator but the JCAPS service wants a "-" separator to consider it a valid Date and so fails. I'm assuming this is done as part of the serialization process when building the SOAP body and was hoping there was a way we could influence it.
I don't normally use SOAP - I try to avoid serialization - but I thought it used ISO 8601? So I thought I'd take a quick look with a SoapFormatter:
public static void ToSoap(Object objToSoap, string filePath) { IFormatter formatter; FileStream fileStream = null; using (fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { formatter = new SoapFormatter(); formatter.Serialize(fileStream, objToSoap); } }
Serializing a DateTime gave a SOAP containing Tick information:
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
SOAP-ENV:Body
<xsd:dateTime id="ref-1">
<ticks>634333105156406250</ticks>
<dateData>9857705142011182058</dateData>
</xsd:dateTime>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>Serializing a class containing a DateTime gave ISO8601 data:
demo d = new demo(); d.s = "Hello Paul"; d.d = DateTime.Now; ToSoap(d, @"F:\\Temp\\datetime.soap"); \[Serializable\] public class demo { public string s; public DateTime d; }
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
SOAP-ENV:Body
<a1:Program_x002B_demo id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/ConsoleApplication1/Tester%2C Version%3D1.0.0.0%2C Culture%3Dneutral%2C PublicKeyToken%3Dnull">
<s id="ref-3">Hello Paul</s>
<d>2011-02-14T19:54:57.4218750+00:00</d>
</a1:Program_x002B_demo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>I know it's not helpful to you, but I'm intrigued now
-
I don't normally use SOAP - I try to avoid serialization - but I thought it used ISO 8601? So I thought I'd take a quick look with a SoapFormatter:
public static void ToSoap(Object objToSoap, string filePath) { IFormatter formatter; FileStream fileStream = null; using (fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { formatter = new SoapFormatter(); formatter.Serialize(fileStream, objToSoap); } }
Serializing a DateTime gave a SOAP containing Tick information:
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
SOAP-ENV:Body
<xsd:dateTime id="ref-1">
<ticks>634333105156406250</ticks>
<dateData>9857705142011182058</dateData>
</xsd:dateTime>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>Serializing a class containing a DateTime gave ISO8601 data:
demo d = new demo(); d.s = "Hello Paul"; d.d = DateTime.Now; ToSoap(d, @"F:\\Temp\\datetime.soap"); \[Serializable\] public class demo { public string s; public DateTime d; }
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
SOAP-ENV:Body
<a1:Program_x002B_demo id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/ConsoleApplication1/Tester%2C Version%3D1.0.0.0%2C Culture%3Dneutral%2C PublicKeyToken%3Dnull">
<s id="ref-3">Hello Paul</s>
<d>2011-02-14T19:54:57.4218750+00:00</d>
</a1:Program_x002B_demo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>I know it's not helpful to you, but I'm intrigued now
I'll get back to you later in the week, I'm off site for a couple of days, thanks for your efforts, much appreciated.