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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. System.Globalization.CultureInfo [modified]

System.Globalization.CultureInfo [modified]

Scheduled Pinned Locked Moved C#
question
3 Posts 3 Posters 0 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.
  • O Offline
    O Offline
    ONeil Tomlinson
    wrote on last edited by
    #1
           string varTempStr = "20100720";
           System.Globalization.CultureInfo enUk = new System.Globalization.CultureInfo("en-GB");
           DateTime billingDate1 = System.DateTime.ParseExact(varTempStr, "yyyyMMdd", enUk);
    
           System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US");
           DateTime billingDate2 = System.DateTime.ParseExact(varTempStr, "yyyyMMdd", enUS);
    
           string x = billingDate1.ToString();
           string y = billingDate2.ToString();
    

    Why are 'x' and 'y' coming back as the same value ("20/07/2010 00:00:00")? Why not x= "20/07/2010 00:00:00" and y = "07/20/2010 00:00:00"?

    modified on Wednesday, August 11, 2010 10:02 AM

    D I 2 Replies Last reply
    0
    • O ONeil Tomlinson
             string varTempStr = "20100720";
             System.Globalization.CultureInfo enUk = new System.Globalization.CultureInfo("en-GB");
             DateTime billingDate1 = System.DateTime.ParseExact(varTempStr, "yyyyMMdd", enUk);
      
             System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US");
             DateTime billingDate2 = System.DateTime.ParseExact(varTempStr, "yyyyMMdd", enUS);
      
             string x = billingDate1.ToString();
             string y = billingDate2.ToString();
      

      Why are 'x' and 'y' coming back as the same value ("20/07/2010 00:00:00")? Why not x= "20/07/2010 00:00:00" and y = "07/20/2010 00:00:00"?

      modified on Wednesday, August 11, 2010 10:02 AM

      D Offline
      D Offline
      Dewald
      wrote on last edited by
      #2

      Because billingDate1 and billingDate2 are just DateTime objects that contain no information on which CultureInfo should be used to output their values. What you are doing when assigning values to the objects (though ParseExact) is simply to specify how varTempStr should be interpreted. But once the DateTime objects have their values they have their values. It is when you export those values to a string that you have to be specific as to how it's supposed to be done. Try:

              string x = billingDate1.ToString(enUk.DateTimeFormat);
              string y = billingDate2.ToString(enUS.DateTimeFormat);
      
      1 Reply Last reply
      0
      • O ONeil Tomlinson
               string varTempStr = "20100720";
               System.Globalization.CultureInfo enUk = new System.Globalization.CultureInfo("en-GB");
               DateTime billingDate1 = System.DateTime.ParseExact(varTempStr, "yyyyMMdd", enUk);
        
               System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US");
               DateTime billingDate2 = System.DateTime.ParseExact(varTempStr, "yyyyMMdd", enUS);
        
               string x = billingDate1.ToString();
               string y = billingDate2.ToString();
        

        Why are 'x' and 'y' coming back as the same value ("20/07/2010 00:00:00")? Why not x= "20/07/2010 00:00:00" and y = "07/20/2010 00:00:00"?

        modified on Wednesday, August 11, 2010 10:02 AM

        I Offline
        I Offline
        Ian Shlasko
        wrote on last edited by
        #3

        Because you're supplying the culture to the parser... So it's using that to change "20100720" into a date. Since you specified an exact format string ("yyyyMMdd"), that culture really doesn't mean much in this case, since both cultures use the same calendar. Then, when you turn the dates back into a string (x and y), you're just using ToString(), so it's using your default culture. You need to specify a culture-specific format provider in the ToString() calls.

        Proud to have finally moved to the A-Ark. Which one are you in?
        Author of the Guardians Saga (Sci-Fi/Fantasy novels)

        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