time format problem

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

    time format problem

    Hi

    I have changed my regional options on my computer to English (United States)
    (I used to have Swedish). I would print the value of the current time in a
    textbox on my asp.net page. I use this function:

    txtTime.Text = DateTime.Now.To ShortTimeString ();

    The time shown on the page is on the format 16:34 and I dont want that. I
    want the american format. What can be wrong?

    Thanks
    Julia
  • Leon Mayne

    #2
    RE: time format problem

    "Julia" wrote:
    txtTime.Text = DateTime.Now.To ShortTimeString ();
    >
    The time shown on the page is on the format 16:34 and I dont want that. I
    want the american format. What can be wrong?
    Set the ShortTimePatter n property of the current culture thread:
    Converts the value of the current DateTime object to its equivalent short time string representation.

    Comment

    • Q. John Chen

      #3
      Re: time format problem

      The code is run on the web server. If the server's regional setting is
      Swedish, the text is still formatted in Swedish. It doesn't matter what
      region the user is using.

      If you want always display US format. You can
      1) change the regional option of the server. or
      2) format the time in us format (something like "hh:mm am/pm" but
      not 100% sure). Or
      3) sepcify the culture to get the time text.

      John


      Julia wrote:
      Hi
      >
      I have changed my regional options on my computer to English (United States)
      (I used to have Swedish). I would print the value of the current time in a
      textbox on my asp.net page. I use this function:
      >
      txtTime.Text = DateTime.Now.To ShortTimeString ();
      >
      The time shown on the page is on the format 16:34 and I dont want that. I
      want the american format. What can be wrong?
      >
      Thanks
      Julia

      Comment

      • Kevin Spencer

        #4
        Re: time format problem

        Hi Julia,

        That is odd. According to the .Net SDK regarding the return value of the
        DateTime.ToShor TimeString method:

        ""The value of this instance is formatted using the short time format
        character, 't'. The return value is identical to the value returned by
        ToString ("t", null)."

        This overload of the DateTime.ToStri ng method takes 2 parameters, the second
        of which is a System.Globaliz ation.DateTimeF ormatInfo instance. If it is
        null, the DateTimeFormatI nfo member of the static
        System.Globaliz ationCurrentCul ture.CultureInf o property is used.

        Again, according to the documentation, this Standard DateTime Format string
        for the "en-us" culture specification is equivalent to the Custom DateTime
        Format string "h:mm tt" (Hour 1-12, 2-digit Minute, and either "AM" or
        "PM").

        The CultureInfo.Cur rentCulture class is documented as, when a managed
        Application Thread starts, the Windows API GetUserDefaultL CID function is
        called, which obtains the Local Identifier from the Operating System. This
        Local Identifier is what you would set in Control Panel if you changed your
        Regional Options.

        The only possibility I can guess is that, since an ASP.Net application is
        hosted in IIS, if you changed your Regional Options and did not either
        restart IIS or reboot the computer, the ASP.Net application may not have
        restarted, and fetched the new Local Identifier.

        Here are some references that should help:


        Provides information about a specific culture (called a locale for unmanaged code development). The information includes the names for the culture, the writing system, the calendar used, the sort order of strings, and formatting for dates and numbers.




        --
        HTH,

        Kevin Spencer
        Microsoft MVP
        Ministry of Software Development
        Thoughts and Ideas about programming, philosophy, science, arts, life, God, and related subjects.


        Never trust a dunderhead with a blunderbuss.


        "Julia" <Julia@discussi ons.microsoft.c omwrote in message
        news:3D7F3236-7EDC-49AD-AB0A-AC5F693BADE4@mi crosoft.com...
        Hi
        >
        I have changed my regional options on my computer to English (United
        States)
        (I used to have Swedish). I would print the value of the current time in a
        textbox on my asp.net page. I use this function:
        >
        txtTime.Text = DateTime.Now.To ShortTimeString ();
        >
        The time shown on the page is on the format 16:34 and I dont want that. I
        want the american format. What can be wrong?
        >
        Thanks
        Julia

        Comment

        • Rad [Visual C# MVP]

          #5
          Re: time format problem

          Hey Julia,

          What you want to do is change the locale for the page to American
          English just before you output the date.

          So what you do is this:

          System.Threadin g.Thread.Curren tThread.Current Culture = new
          System.Globaliz ation.CultureIn fo("en-US");
          txtTime.Text = DateTime.Now.To ShortTimeString ();

          On Thu, 23 Nov 2006 07:40:02 -0800, Julia
          <Julia@discussi ons.microsoft.c omwrote:
          >Hi
          >
          >I have changed my regional options on my computer to English (United States)
          >(I used to have Swedish). I would print the value of the current time in a
          >textbox on my asp.net page. I use this function:
          >
          >txtTime.Text = DateTime.Now.To ShortTimeString ();
          >
          >The time shown on the page is on the format 16:34 and I dont want that. I
          >want the american format. What can be wrong?
          >
          >Thanks
          >Julia
          --

          Bits.Bytes.

          Comment

          • Julia

            #6
            Re: time format problem

            Hi

            and thanks for all the answers!

            Finally I added these to my config file:

            <globalizatio n
            requestEncoding ="utf-8"
            responseEncodin g="utf-8"
            culture="en-US "
            uiCulture="en-US "
            />
            This section will force the application to use the american time format, so
            this will solve my problem.

            The strange thing is that I run everything on my local machine (iis, sql,
            client) and I did change my regional options to English but still the format
            was in Swedish. Maybe I didnt restart my computer so I will try that today.

            Thanks
            Julia

            "Rad [Visual C# MVP]" wrote:
            Hey Julia,
            >
            What you want to do is change the locale for the page to American
            English just before you output the date.
            >
            So what you do is this:
            >
            System.Threadin g.Thread.Curren tThread.Current Culture = new
            System.Globaliz ation.CultureIn fo("en-US");
            txtTime.Text = DateTime.Now.To ShortTimeString ();
            >
            On Thu, 23 Nov 2006 07:40:02 -0800, Julia
            <Julia@discussi ons.microsoft.c omwrote:
            >
            Hi

            I have changed my regional options on my computer to English (United States)
            (I used to have Swedish). I would print the value of the current time in a
            textbox on my asp.net page. I use this function:

            txtTime.Text = DateTime.Now.To ShortTimeString ();

            The time shown on the page is on the format 16:34 and I dont want that. I
            want the american format. What can be wrong?

            Thanks
            Julia
            --
            >
            Bits.Bytes.

            >

            Comment

            Working...