简体   繁体   中英

Digital clock -

I would like to display a digital clock.Here is what i have to display the time in a textblock which is in 24 hour format that i dont know how to convert to 12 hours.

DispatcherTimer timerdigital;

    public MainWindow()
    {
        this.InitializeComponent();
        //StartTimer(null, null);

        timerdigital = new DispatcherTimer();
        timerdigital.Interval = TimeSpan.FromSeconds(1.0);
        timerdigital.Start();
        timerdigital.Tick += new EventHandler(delegate(object s, EventArgs a)
        {
            tbDigital.Text = "" + DateTime.Now.Hour + ":"
          + DateTime.Now.Minute + ":"
          + DateTime.Now.Second;


        });

thank you

Don't know about the digital-like display, but for switching to 12-hour clock from 24-hour the easiest approach would be to use DateTime string formatting:

timerdigital.Tick += new EventHandler(delegate(object s, EventArgs a)
        {
            tbDigital.Text = DateTime.Now.ToString("hh:mm:ss tt");
        });

Here hh is the hours in 12-hour format, mm is minutes, ss is seconds, and tt is the AM/PM marker.

See more about Date and Time format strings here .

Here is a nice ready to use source code for wpf:
http://www.codeproject.com/KB/WPF/digitalclock.aspx
About the 12/24 hour format:

 int smallhour = (Hour > 12) ? Hour - 12 : Hour;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM