Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. CultureInfo and DateTime.parse

CultureInfo and DateTime.parse

Scheduled Pinned Locked Moved C#
wcfdata-structuresxmljsonquestion
9 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul E Davies
    wrote on last edited by
    #1

    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.

    OriginalGriffO 1 Reply Last reply
    0
    • P Paul E Davies

      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.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      P 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

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

        P Offline
        P Offline
        Paul E Davies
        wrote on last edited by
        #3

        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

        OriginalGriffO 1 Reply Last reply
        0
        • P Paul E Davies

          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

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

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

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          P 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

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

            P Offline
            P Offline
            Paul E Davies
            wrote on last edited by
            #5

            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

            OriginalGriffO 1 Reply Last reply
            0
            • P Paul E Davies

              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

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

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

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              P 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

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

                P Offline
                P Offline
                Paul E Davies
                wrote on last edited by
                #7

                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.

                OriginalGriffO 1 Reply Last reply
                0
                • P Paul E Davies

                  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.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  P 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    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

                    P Offline
                    P Offline
                    Paul E Davies
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups