Date format causing great problems!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jozef Jarosciak

    Date format causing great problems!

    I have an application which imports fields from external source where
    date format of one the collumns is English (United States): M/d/yyyy.
    So July 1, 2005 equals to: 7/1/05
    I am using this date everywhere in the application.

    Problem is when someones is lets say in Portugal with the regional
    settings for date set to DD-MM-YYYY.

    I am going crazy to figure out how to make everyone to use M/d/yyyy
    scheme without changing their regional settings, as lots of people
    simply can't do that.

    Can someone point me to what I could do, or post some code on how to go
    around this annoying problem?

    It would be very much appreciated.

    Joe

  • Brett

    #2
    Re: Date format causing great problems!


    "Jozef Jarosciak" <joe@doprocess. com> wrote in message
    news:1121535808 .312505.309850@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    >I have an application which imports fields from external source where
    > date format of one the collumns is English (United States): M/d/yyyy.
    > So July 1, 2005 equals to: 7/1/05
    > I am using this date everywhere in the application.
    >
    > Problem is when someones is lets say in Portugal with the regional
    > settings for date set to DD-MM-YYYY.
    >
    > I am going crazy to figure out how to make everyone to use M/d/yyyy
    > scheme without changing their regional settings, as lots of people
    > simply can't do that.
    >
    > Can someone point me to what I could do, or post some code on how to go
    > around this annoying problem?
    >
    > It would be very much appreciated.
    >
    > Joe
    >[/color]

    One way to is to detect if the date coming in is non US and then do some
    type of transforming on it. You mention external source. Is there
    something coming with the data to signify its country or at least that it is
    non US? What is the data type they are sending back?

    Use some of the DateTime methods to return a date against your imported
    date. Then you can go further to pick it apart by hours, minutes, month,
    year if you need to. From that you can construct the date format you need
    or possibly just format off of the returned date if DateTime allows.

    Brett


    Comment

    • Jozef Jarosciak

      #3
      Re: Date format causing great problems!

      Hi Brett,
      I need some one liner, code which would set the whole program (form) to
      "M/d/yyyy" so whenever I do have to deal with date it would forget
      about my users DD-MM-YYYY format and assume they all have "M/d/yyyy".
      Is there any way to ignore regional settings and its date format?
      Joe

      Comment

      • Brett

        #4
        Re: Date format causing great problems!


        "Jozef Jarosciak" <joe@doprocess. com> wrote in message
        news:1121537781 .026795.306340@ g44g2000cwa.goo glegroups.com.. .[color=blue]
        > Hi Brett,
        > I need some one liner, code which would set the whole program (form) to
        > "M/d/yyyy" so whenever I do have to deal with date it would forget
        > about my users DD-MM-YYYY format and assume they all have "M/d/yyyy".
        > Is there any way to ignore regional settings and its date format?
        > Joe
        >[/color]

        Have you tried
        DateTime.Parse( TextBox1.Text). ToString("M/d/yyyy");

        Are users editing this data in the form or does it appear from you import as
        read only to them?

        Brett


        Comment

        • Jeffrey Sax

          #5
          Re: Date format causing great problems!

          It is usually best to let users enter dates (and numbers) in the format they
          are most familiar with. Trying to force them to do otherwise will only lead
          to data entry errors.

          The date format only matters when your application interacts with the
          'outside world', i.e. your users or the database. Within the application,
          the DateTime variables are independent of their value's textual
          representation.

          In your external interactions, use the appropriate CultureInfo object, which
          you can then pass as the IFormatProvider argument to the DateTime's Parse or
          ToString methods as appropriate.

          The current user's CultureInfo is available through the CurrentUICultur e
          property of CultureInfo. It sounds like your database uses a specific
          culture, for example: "new CultureInfo("EN-US")"

          For more information, including code samples, look up these class and
          interface names in the .NET framework documentation.

          Jeffrey Sax
          Extreme Optimization
          Math and statistics libraries for the .NET framework. Develop financial, statistical, scientific and engineering applications faster in C#, F# or Visual Basic.NET.


          "Jozef Jarosciak" <joe@doprocess. com> wrote in message
          news:1121535808 .312505.309850@ o13g2000cwo.goo glegroups.com.. .[color=blue]
          >I have an application which imports fields from external source where
          > date format of one the collumns is English (United States): M/d/yyyy.
          > So July 1, 2005 equals to: 7/1/05
          > I am using this date everywhere in the application.
          >
          > Problem is when someones is lets say in Portugal with the regional
          > settings for date set to DD-MM-YYYY.
          >
          > I am going crazy to figure out how to make everyone to use M/d/yyyy
          > scheme without changing their regional settings, as lots of people
          > simply can't do that.
          >
          > Can someone point me to what I could do, or post some code on how to go
          > around this annoying problem?
          >
          > It would be very much appreciated.
          >
          > Joe
          >[/color]


          Comment

          • Brett

            #6
            Re: Date format causing great problems!

            One example of what Jeffrey is talking about is:

            IFormatProvider culture = new CultureInfo("en-US", true);
            string DateTimeEnValue = DateTime.Now.To String();
            string myShortEnglishD ate = DateTimeEnValue .ToShortDateStr ing();

            Brett

            "Jeffrey Sax" <info@extremeop timization.com> wrote in message
            news:E76dnftxec T34UTfRVn-2g@rogers.com.. .[color=blue]
            > It is usually best to let users enter dates (and numbers) in the format
            > they are most familiar with. Trying to force them to do otherwise will
            > only lead to data entry errors.
            >
            > The date format only matters when your application interacts with the
            > 'outside world', i.e. your users or the database. Within the application,
            > the DateTime variables are independent of their value's textual
            > representation.
            >
            > In your external interactions, use the appropriate CultureInfo object,
            > which you can then pass as the IFormatProvider argument to the DateTime's
            > Parse or ToString methods as appropriate.
            >
            > The current user's CultureInfo is available through the CurrentUICultur e
            > property of CultureInfo. It sounds like your database uses a specific
            > culture, for example: "new CultureInfo("EN-US")"
            >
            > For more information, including code samples, look up these class and
            > interface names in the .NET framework documentation.
            >
            > Jeffrey Sax
            > Extreme Optimization
            > http://www.extremeoptimization.com
            >
            > "Jozef Jarosciak" <joe@doprocess. com> wrote in message
            > news:1121535808 .312505.309850@ o13g2000cwo.goo glegroups.com.. .[color=green]
            >>I have an application which imports fields from external source where
            >> date format of one the collumns is English (United States): M/d/yyyy.
            >> So July 1, 2005 equals to: 7/1/05
            >> I am using this date everywhere in the application.
            >>
            >> Problem is when someones is lets say in Portugal with the regional
            >> settings for date set to DD-MM-YYYY.
            >>
            >> I am going crazy to figure out how to make everyone to use M/d/yyyy
            >> scheme without changing their regional settings, as lots of people
            >> simply can't do that.
            >>
            >> Can someone point me to what I could do, or post some code on how to go
            >> around this annoying problem?
            >>
            >> It would be very much appreciated.
            >>
            >> Joe
            >>[/color]
            >
            >[/color]


            Comment

            • Jozef Jarosciak

              #7
              Re: Date format causing great problems!

              I am sorry, but this is a spanish village for me.
              Can someone show me on simple example how I force my whole application
              to use en-US format?
              I don't use threads in my program, so I can't apply cultureinfo to
              thread.

              Imagine this.
              Simple case scenario:
              I ask for datetime.now and it gives me: 7/1/2005
              But my user gets: 1/7/2005
              So when I further ask for day of the week, which I want to highlight,
              it is all screwed up, as it thinks it's 7th of January as opose to
              correct 1 of July.

              I need a way to force the whole program to think user is in USA with US
              regional settings for date.

              Is there a way to do this?
              Joe

              Comment

              • Jozef Jarosciak

                #8
                Re: Date format causing great problems!

                This is a fix I found in vb.net and worked like a charm for me.


                Dim ci As New System.Globaliz ation.CultureIn fo("en-US", False)
                Dim newCi As System.Globaliz ation.CultureIn fo =
                CType(ci.Clone( ), System.Globaliz ation.CultureIn fo)
                newCi.DateTimeF ormat.AMDesigna tor = "AM"
                newCi.DateTimeF ormat.PMDesigna tor = "PM"
                newCi.DateTimeF ormat.ShortDate Pattern = "M/d/yyyy"
                Thread.CurrentT hread.CurrentCu lture = newCi

                Just wanted to post it and share it with other. Now my whole
                application is switched to en-US format, no matter what country is my
                user from.
                Joe

                Comment

                • Brett

                  #9
                  Re: Date format causing great problems!

                  What search keywords did you use to find the code?

                  Nice that you got it.

                  Brett

                  "Jozef Jarosciak" <joe@doprocess. com> wrote in message
                  news:1121560152 .324288.134750@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                  > This is a fix I found in vb.net and worked like a charm for me.
                  >
                  >
                  > Dim ci As New System.Globaliz ation.CultureIn fo("en-US", False)
                  > Dim newCi As System.Globaliz ation.CultureIn fo =
                  > CType(ci.Clone( ), System.Globaliz ation.CultureIn fo)
                  > newCi.DateTimeF ormat.AMDesigna tor = "AM"
                  > newCi.DateTimeF ormat.PMDesigna tor = "PM"
                  > newCi.DateTimeF ormat.ShortDate Pattern = "M/d/yyyy"
                  > Thread.CurrentT hread.CurrentCu lture = newCi
                  >
                  > Just wanted to post it and share it with other. Now my whole
                  > application is switched to en-US format, no matter what country is my
                  > user from.
                  > Joe
                  >[/color]


                  Comment

                  • Mihai N.

                    #10
                    Re: Date format causing great problems!

                    > Can someone show me on simple example how I force my whole application[color=blue]
                    > to use en-US format?[/color]
                    Sorry, but what all are trying to explaint is that this is really
                    a bad thing to do.


                    --
                    Mihai Nita [Microsoft MVP, Windows - SDK]
                    ------------------------------------------
                    Replace _year_ with _ to get the real email

                    Comment

                    Working...