Skip to content

Displaying actual time in a form?

Gabriel Gonzalez

Asked Jan 16, 2006, 9:28 PM EST4 answers
Share
LinkedInX

Hello,

I am a complete beginner and am building my first app.
I am trying to show the actual time in my form, and so far I can display it ok using the DateTime structure and it's methods. The problem I have is: I don't know how to make it update automatically.

Any help will be greatly appreciated.

Thanks,

Gabriel

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    You can add a Timer Control; It's Interval property can be set 1000;
    then you can put your code of displaying current time in its Tick Event.

    That's ok!
    0
  • 0

    public string GetTime()

    {

    string TimeInString = "";

    // Get current time

    int hour = DateTime.Now.Hour;

    int min = DateTime.Now.Minute;

    int sec = DateTime.Now.Second;

    // Format current time into string

    TimeInString = (hour < 10) ? "0" + hour.ToString() : hour.ToString();

    TimeInString += ":" + ( (min<10) ? "0" + min.ToString() : min.ToString() );

    TimeInString += ":" + ( (sec<10) ? "0" + sec.ToString() : sec.ToString() );

    return TimeInString;

    }

    private void timer1_Tick(object sender, System.EventArgs e)

    {

    if( sender == timer1 )

    {

    label1.Text = GetTime();

    Invalidate();

    }

    }

    put this code in.. and set ur Timer interval as 1000.. Hope its help..

    0
  • 0

    Thank you very much for your replies. I will try them later today but I am sure this is what I am looking for.
    This site is great, I really appreciate your help.

    Forgive me if I ask any more 'dumb' questions. But self learning C is not as easy as it would seem. There is an overload of information on the web and it is easy to get lost and confused.

    When searching for info I always get thousands of hits, really hard to sort thru.

    Thanks again,

    Gabriel

    0
  • 0

    Here's much more elegant way to format datetime to string:

    C#
    using System;
    
    using 
    System.Collections.Generic;
    
    using System.Text;
    
    
    namespace 
    ConsoleApplication1
    
    {
    
        class
    Program
    
        {
    
            
    static void 
    Main(string[] args)
    
            {
    
                
    DateTime now = 
    DateTime.Now;
    
    
                
     //gets the short time format
    
                
    string shortTime = now.ToShortTimeString();
    
                
    //gets your special time format, more info
    
                //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandarddatetimeformatstrings.asp
    
                
    string specialTime = now.ToString("hh:mm:ss");
    
                
    //gets the long datetime format corresponding to 
    your locality settings
    
                
    string localizedDateTime = 
    now.ToString( System.Threading.Thread.CurrentThread.CurrentCulture);
    
    
                
    Console.WriteLine(shortTime);
    
                
    Console.WriteLine(specialTime);
    
                
    Console.WriteLine(localizedDateTime);
    
            }
    
        }
    
    }

    Much more formatting options awailable in DateTime class...

    0