DateTime.ToShortTimeString and 24 hour time

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

    DateTime.ToShortTimeString and 24 hour time

    Hello-

    I live in the US, however I'm attempting to make a product of mine more
    international-friendly. There are a number of instances where a calendar
    function of my program displays various times, which is displayed using
    DateTime.ToShor tTimeString(). This always displays them in an AM/PM format.
    When I go into the test computer's locality and change the time format to 24
    hour time, the program still displays times in AM/PM format. Is there a
    better way to implement this, so that either AM/PM or 24 hour time will be
    displayed based upon the user's locality? Thank you.

    Robert


  • Morten Wennevik

    #2
    Re: DateTime.ToShor tTimeString and 24 hour time

    Hi Robert,

    Are you sure you are changing the time correctly. Remember, to get rid of the AM/PM symbol you need to remove the 'tt' part. Also, just changing the time does not appear to affect the program, you need to rebuild it (not just hit F5, which will do nothing if you haven't changed the code).

    To test how it would appear it different localizations you can programmaticall y change the programs "location".

    Thread.CurrentT hread.CurrentCu lture = new CultureInfo("nb-NO");
    // Norwegian bokmål, uses a 24 hour clock


    Happy coding!
    Morten Wennevik [C# MVP]

    Comment

    • Robert Misiak

      #3
      Re: DateTime.ToShor tTimeString and 24 hour time

      Hello Morten:

      Thanks for your reply. I'm pretty sure that I changed the time format
      correctly on the test system as changing it changed the time shown in the
      system tray to 24 hour format. However, my goal is not to rebuild many
      different versions of the program for use in different localities; I'd like
      the program to be able to determine the local time format (as configured in
      the Control Panel at least) and display it accordingly. Is this stored
      somewhere in the registry?

      Robert


      "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
      news:opr76jv3ol klbvpo@morten_x .edunord...[color=blue]
      > Hi Robert,
      >
      > Are you sure you are changing the time correctly. Remember, to get rid of[/color]
      the AM/PM symbol you need to remove the 'tt' part. Also, just changing the
      time does not appear to affect the program, you need to rebuild it (not just
      hit F5, which will do nothing if you haven't changed the code).[color=blue]
      >
      > To test how it would appear it different localizations you can[/color]
      programmaticall y change the programs "location".[color=blue]
      >
      > Thread.CurrentT hread.CurrentCu lture = new CultureInfo("nb-NO");
      > // Norwegian bokmål, uses a 24 hour clock
      >
      >
      > Happy coding!
      > Morten Wennevik [C# MVP][/color]


      Comment

      • Morten Wennevik

        #4
        Re: DateTime.ToShor tTimeString and 24 hour time

        Hm, odd, DateTime.ToShor tTimeString seems to ignore customized time formats alltogether. Rebuilding won't matter.
        If you change your current country settings the program should detect it fine without the need for a rebuild.

        Happy coding!
        Morten Wennevik [C# MVP]

        Comment

        • Robert Misiak

          #5
          Re: DateTime.ToShor tTimeString and 24 hour time

          You're right. Initially I kept the locality as US/English and only changed
          the time format (which should have worked anyway, but that's another story.)
          I just now changed the locality to Norwegian (Bokmal) and it displayed the
          times correctly, and even displayed the names of the months in Norwegian, as
          I had hoped. Guess we already are international-friendly. :-) Thanks for
          your help.

          Robert



          "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
          news:opr76mf9b0 klbvpo@morten_x .edunord...[color=blue]
          > Hm, odd, DateTime.ToShor tTimeString seems to ignore customized time[/color]
          formats alltogether. Rebuilding won't matter.[color=blue]
          > If you change your current country settings the program should detect it[/color]
          fine without the need for a rebuild.[color=blue]
          >
          > Happy coding!
          > Morten Wennevik [C# MVP][/color]


          Comment

          • Marcin Grzêbski

            #6
            Re: DateTime.ToShor tTimeString and 24 hour time

            Hi Robert!
            [color=blue]
            > Hello-
            >
            > I live in the US, however I'm attempting to make a product of mine more
            > international-friendly. There are a number of instances where a calendar
            > function of my program displays various times, which is displayed using
            > DateTime.ToShor tTimeString(). This always displays them in an AM/PM format.
            > When I go into the test computer's locality and change the time format to 24
            > hour time, the program still displays times in AM/PM format. Is there a
            > better way to implement this, so that either AM/PM or 24 hour time will be
            > displayed based upon the user's locality? Thank you.
            >
            > Robert[/color]

            Use DataTime custom formatting!
            There's no philosophy in time format, so i can recomend
            to use this:

            DateTime.ToStri ng("HH:mm:ss") ;
            // besides of DateTime.ToStri ng("hh:mm:ss") ;

            For more info, look in .NET docs for "DateTimeFormat Info"

            Cheers!

            Marcin

            Comment

            • Morten Wennevik

              #7
              Re: DateTime.ToShor tTimeString and 24 hour time

              > DateTime.ToStri ng("HH:mm:ss") ;[color=blue]
              > // besides of DateTime.ToStri ng("hh:mm:ss") ;[/color]

              Would you know how to add the AM/PM part?
              DateTime.ToStri ng("hh:mm:ss tt") won't do anything since I have no defined AM or PM symbols in the regional settings.


              Happy coding!
              Morten Wennevik [C# MVP]

              Comment

              • Ed Courtenay

                #8
                Re: DateTime.ToShor tTimeString and 24 hour time

                Morten Wennevik wrote:
                [color=blue][color=green]
                >> DateTime.ToStri ng("HH:mm:ss") ;
                >> // besides of DateTime.ToStri ng("hh:mm:ss") ;[/color]
                >
                >
                > Would you know how to add the AM/PM part?
                > DateTime.ToStri ng("hh:mm:ss tt") won't do anything since I have no
                > defined AM or PM symbols in the regional settings.
                >
                >
                > Happy coding!
                > Morten Wennevik [C# MVP][/color]

                You simply don't need to do this. Try the following code:

                public static void Main()
                {
                string dateString = "Tue, 27 Apr 2004 08:16:13 GMT";
                DateTime dateTime = DateTime.Parse( dateString);

                Thread.CurrentT hread.CurrentCu lture = new CultureInfo("en-US");
                Console.WriteLi ne(Thread.Curre ntThread.Curren tCulture.Englis hName);
                Console.WriteLi ne("{0}", dateTime.ToStri ng("T"));

                Thread.CurrentT hread.CurrentCu lture = new CultureInfo("de-DE");
                Console.WriteLi ne(Thread.Curre ntThread.Curren tCulture.Englis hName);
                Console.WriteLi ne("{0}", dateTime.ToStri ng("T"));

                Console.ReadLin e();
                }

                --

                Ed Courtenay
                [MCP, MCSD]

                Comment

                Working...