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. display the machine time in a textbox

display the machine time in a textbox

Scheduled Pinned Locked Moved C#
csharp
10 Posts 6 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.
  • C Offline
    C Offline
    costavo
    wrote on last edited by
    #1

    hi, I want to display the time and date in a textbox using c# where i should put the formula and how regards

    N 1 Reply Last reply
    0
    • C costavo

      hi, I want to display the time and date in a textbox using c# where i should put the formula and how regards

      N Offline
      N Offline
      N a r e s h P a t e l
      wrote on last edited by
      #2

      Use System.DateTime.Now textbox.Text=System.DateTime.Now.ToString(); at page load or any buttion's click event wherever you want.

      Naresh Patel

      Y 1 Reply Last reply
      0
      • N N a r e s h P a t e l

        Use System.DateTime.Now textbox.Text=System.DateTime.Now.ToString(); at page load or any buttion's click event wherever you want.

        Naresh Patel

        Y Offline
        Y Offline
        yoaz
        wrote on last edited by
        #3

        exactly. and use a timer if u want it to look like a clock:)

        there are no facts, only interpretations

        M 1 Reply Last reply
        0
        • Y yoaz

          exactly. and use a timer if u want it to look like a clock:)

          there are no facts, only interpretations

          M Offline
          M Offline
          Muammar
          wrote on last edited by
          #4

          And use the conditional operator ?: to format it like a real clock:)

                  int Hours = ((YourTimeVariable / 1000) / 3600);
                  int Minutes = ((YourTimeVariable / 1000) / 60) - (Hours \* 60);
                  int Seconds = ((((YourTimeVariable / 1000)) - (Minutes \* 60)) - (Hours \* 3600));
          
                  strHours = Hours < 10 ? "0" + Hours.ToString() : Hours.ToString();
                  strMinutes = Minutes < 10 ? "0" + Minutes.ToString() : Minutes.ToString();
                  strSeconds = Seconds < 10 ? "0" + Seconds.ToString() : Seconds.ToString();
          
                  Time\_TextBox.Text = strHours + ":" + strMinutes + ":" + strSeconds;
          

          Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

          L N Y 3 Replies Last reply
          0
          • M Muammar

            And use the conditional operator ?: to format it like a real clock:)

                    int Hours = ((YourTimeVariable / 1000) / 3600);
                    int Minutes = ((YourTimeVariable / 1000) / 60) - (Hours \* 60);
                    int Seconds = ((((YourTimeVariable / 1000)) - (Minutes \* 60)) - (Hours \* 3600));
            
                    strHours = Hours < 10 ? "0" + Hours.ToString() : Hours.ToString();
                    strMinutes = Minutes < 10 ? "0" + Minutes.ToString() : Minutes.ToString();
                    strSeconds = Seconds < 10 ? "0" + Seconds.ToString() : Seconds.ToString();
            
                    Time\_TextBox.Text = strHours + ":" + strMinutes + ":" + strSeconds;
            

            Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Muammar© wrote:

            strHours = Hours < 10 ? "0" + Hours.ToString() : Hours.ToString();

            strHours = Hours.ToString("D2"); etc. will suffice.

            Luc Pattyn [Forum Guidelines] [My Articles]


            this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


            M 1 Reply Last reply
            0
            • M Muammar

              And use the conditional operator ?: to format it like a real clock:)

                      int Hours = ((YourTimeVariable / 1000) / 3600);
                      int Minutes = ((YourTimeVariable / 1000) / 60) - (Hours \* 60);
                      int Seconds = ((((YourTimeVariable / 1000)) - (Minutes \* 60)) - (Hours \* 3600));
              
                      strHours = Hours < 10 ? "0" + Hours.ToString() : Hours.ToString();
                      strMinutes = Minutes < 10 ? "0" + Minutes.ToString() : Minutes.ToString();
                      strSeconds = Seconds < 10 ? "0" + Seconds.ToString() : Seconds.ToString();
              
                      Time\_TextBox.Text = strHours + ":" + strMinutes + ":" + strSeconds;
              

              Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              Or maybe just use the ToString method :rolleyes: DateTime.Now.ToString("hh:mm:ss");


              only two letters away from being an asset

              M 2 Replies Last reply
              0
              • N Not Active

                Or maybe just use the ToString method :rolleyes: DateTime.Now.ToString("hh:mm:ss");


                only two letters away from being an asset

                M Offline
                M Offline
                Muammar
                wrote on last edited by
                #7

                Mark Nischalke wrote:

                DateTime.Now.ToString("hh:mm:ss");

                DOH!! :doh: Thank you guys:laugh:


                Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

                1 Reply Last reply
                0
                • L Luc Pattyn

                  Muammar© wrote:

                  strHours = Hours < 10 ? "0" + Hours.ToString() : Hours.ToString();

                  strHours = Hours.ToString("D2"); etc. will suffice.

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                  M Offline
                  M Offline
                  Muammar
                  wrote on last edited by
                  #8

                  Have I been re-invinting the wheel? :laugh: Thanks Luc:).. I didnt' know it.


                  Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

                  1 Reply Last reply
                  0
                  • N Not Active

                    Or maybe just use the ToString method :rolleyes: DateTime.Now.ToString("hh:mm:ss");


                    only two letters away from being an asset

                    M Offline
                    M Offline
                    Muammar
                    wrote on last edited by
                    #9

                    By the way, your sig made me laugh so much a couple of months ago,yet I want to know if my understanding was correct so please tell me what it really means:->


                    Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

                    1 Reply Last reply
                    0
                    • M Muammar

                      And use the conditional operator ?: to format it like a real clock:)

                              int Hours = ((YourTimeVariable / 1000) / 3600);
                              int Minutes = ((YourTimeVariable / 1000) / 60) - (Hours \* 60);
                              int Seconds = ((((YourTimeVariable / 1000)) - (Minutes \* 60)) - (Hours \* 3600));
                      
                              strHours = Hours < 10 ? "0" + Hours.ToString() : Hours.ToString();
                              strMinutes = Minutes < 10 ? "0" + Minutes.ToString() : Minutes.ToString();
                              strSeconds = Seconds < 10 ? "0" + Seconds.ToString() : Seconds.ToString();
                      
                              Time\_TextBox.Text = strHours + ":" + strMinutes + ":" + strSeconds;
                      

                      Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

                      Y Offline
                      Y Offline
                      yoaz
                      wrote on last edited by
                      #10

                      or be lazy and use DateTime.ToLongTimeString ( alternatively :DateTime.ToShortTimeString ) methods

                      there are no facts, only interpretations

                      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